Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////ПЪРВА ЗАДАЧА/////////////////////////////////////////////////////////
- package Prog;
- import java.util.HashMap;
- import java.util.Map;
- public class Prog_Main {
- private static HashMap<Integer, Integer> map = new HashMap();
- public static void getValue() {
- int freq = 0;
- int key = 0;
- for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
- if (freq < entry.getValue()) {
- freq = entry.getValue();
- key = entry.getKey();
- }
- }
- System.out.println("Most frequent "+key + " ==> " + freq);
- }
- public static void HashMapFill(int[] arr) {
- for (int i = 0; i < arr.length; i++) {
- if (!map.containsKey(arr[i])) {
- map.put(arr[i], 1);
- } else if (map.containsKey(arr[i])) {
- int count = 1 + map.get(arr[i]);
- map.put(arr[i], count);
- }
- }
- }
- public static void main(String[] args) {
- int arr[] = {4, 2, 7, 7, 7, 2, 2, 2, 7, 3, 6, 1};
- HashMapFill(arr);
- System.out.println(map);
- getValue();
- }
- }
Add Comment
Please, Sign In to add comment