# Occurence of digits

• there is an Array of length n containing integers. you have to find which digit occur maximum time in array and also how many times it occurs.
e.g. [12, 45, 2, 12, 19, 23, 34, 2 19, 12, 99, 100, 2, 19, 12, 67, 23, 12] then answer should be number 12 occurs 5 times.

• This is basically a problem of sorting a HashMap by value ;
Initially ,I stored the occurrence of each digit in HashMap (where key denotes the number and value denotes its occurrence ). As we know each item in a Map is an Entry .
So, create a new List and initialise it with entries of HashMap .
Then use Collections.sort() to sort the entries while using Comparator for comparing the values of two entries while sorting .
I have sorted the list in decreasing order so element at top is the required value .

``````import java.util.*;
public class Occur {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n=sc.nextInt();
int ar[]=new int[n+1];
for (int i=0;i<n;i++)
{
ar[i]=sc.nextInt();
}
Map<Integer,Integer> map=new HashMap<>();
for (int i=0;i<n;i++)
{
int val=ar[i];
if (map.containsKey(val))
{
map.put(val,map.get(val)+1);
}
else
{
map.put(val,1);
}
}