Advertisement
EmiliaKitkarska

Zadacha1

Apr 19th, 2021
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. 1. Най-често срещано число
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class Zadachki {
  7.  
  8.     private static HashMap<Integer, Integer> map = new HashMap();
  9.  
  10.     public static void getValue() {
  11.         int com = 0;
  12.         int key = 0;
  13.         for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
  14.             if (com < entry.getValue()) {
  15.                 com = entry.getValue();
  16.                 key = entry.getKey();
  17.             }
  18.         }
  19.         System.out.println("Most common "+key + " ==> " + com);
  20.     }
  21.  
  22.     public static void HashMapFill(int[] arr) {
  23.         for (int i = 0; i < arr.length; i++) {
  24.             if (!map.containsKey(arr[i])) {
  25.                 map.put(arr[i], 1);
  26.             } else if (map.containsKey(arr[i])) {
  27.                 int count = 1 + map.get(arr[i]);
  28.                 map.put(arr[i], count);
  29.             }
  30.         }
  31.     }
  32.  
  33.     public static void main(String[] args) {
  34.         int arr[] = {4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3};
  35.         HashMapFill(arr);
  36.         System.out.println(map);
  37.         getValue();
  38.  
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement