java program to find the max and min occurrence of element in an array using Map


import java.util.*;

class CountOccurrenceOfElement
{
public static void main(String[] args)
{
int n[]={1,2,3,2,5,6,3,2,1,3,2,3,3};
Map<Integer,Integer> map=new HashMap<Integer,Integer>();
for(Integer i:n)
{
if(map.containsKey(i))
{
map.put(i,map.get(i)+1);
}
else
{
map.put(i,1);
}
}
for(Map.Entry m:map.entrySet())
{
System.out.println(m.getKey()+" comes "+m.getValue()+" times.");
}
//find the max value of the map and then print the key of map
int max=Collections.max(map.values());
int min=Collections.min(map.values());
System.out.println(max);
System.out.println(min);
for(Map.Entry m:map.entrySet())
{
if(m.getValue().equals(max))
{
System.out.println(m.getKey()+" comes maximum "+m.getValue()+" times.");
}
//here we are printing which element comes max times.
if(m.getValue().equals(min))
{
System.out.println(m.getKey()+" comes minimum "+m.getValue()+" times");
}
//here we are printing which key comes minimum times.
}
}
}