frequency count in java using hashmap
static void countFreq(int arr[], int n)
{
Map<Integer, Integer> mp = new HashMap<>();
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++)
{
if (mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1);
}
else
{
mp.put(arr[i], 1);
}
}
// Traverse through map and print frequencies
for (Map.Entry<Integer, Integer> entry : mp.entrySet())
{
System.out.println(entry.getKey() + " " + entry.getValue());
}
}