BetinaUKTC

zadacha1

Jan 14th, 2021 (edited)
761
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////ПЪРВА ЗАДАЧА/////////////////////////////////////////////////////////
  2. package Prog;
  3.  
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. public class Prog_Main {
  8.  
  9.     private static HashMap<Integer, Integer> map = new HashMap();
  10.  
  11.     public static void getValue() {
  12.         int freq = 0;
  13.         int key = 0;
  14.         for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
  15.             if (freq < entry.getValue()) {
  16.                 freq = entry.getValue();
  17.                 key = entry.getKey();
  18.             }
  19.         }
  20.         System.out.println("Most frequent "+key + " ==> " + freq);
  21.     }
  22.  
  23.     public static void HashMapFill(int[] arr) {
  24.         for (int i = 0; i < arr.length; i++) {
  25.             if (!map.containsKey(arr[i])) {
  26.                 map.put(arr[i], 1);
  27.             } else if (map.containsKey(arr[i])) {
  28.                 int count = 1 + map.get(arr[i]);
  29.                 map.put(arr[i], count);
  30.             }
  31.         }
  32.     }
  33.  
  34.     public static void main(String[] args) {
  35.         int arr[] = {4, 2, 7, 7, 7, 2, 2, 2, 7, 3, 6, 1};
  36.         HashMapFill(arr);
  37.         System.out.println(map);
  38.         getValue();
  39.  
  40.     }
  41. }
  42.  
  43.  
RAW Paste Data