frequency count in java using hashmap

Solutions on MaxInterview for frequency count in java using hashmap by the best coders in the world

showing results for - "frequency count in java using hashmap"
Rafael
05 Jan 2018
1static void countFreq(int arr[], int n)
2    {
3        Map<Integer, Integer> mp = new HashMap<>();
4 
5        // Traverse through array elements and
6        // count frequencies
7        for (int i = 0; i < n; i++)
8        {
9            if (mp.containsKey(arr[i]))
10            {
11                mp.put(arr[i], mp.get(arr[i]) + 1);
12            }
13            else
14            {
15                mp.put(arr[i], 1);
16            }
17        }
18        // Traverse through map and print frequencies
19        for (Map.Entry<Integer, Integer> entry : mp.entrySet())
20        {
21            System.out.println(entry.getKey() + " " + entry.getValue());
22        }
23    }