fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc=new Scanner(System.in);
  14. int n=sc.nextInt();
  15. int a[]=new int[n];
  16.  
  17. for(int i=0;i<n;i++)
  18. a[i]=sc.nextInt();
  19.  
  20. Map<Integer,Integer> hash=new HashMap<>();
  21. int max=-9999, min=99999,maxele=0,minele=0;
  22. for(int ele:a)
  23. {
  24. hash.put(ele,hash.getOrDefault(ele,0)+1);
  25. }
  26.  
  27. for(int ele: a)
  28. {
  29. if(max<=hash.get(ele))
  30. {
  31. max=hash.get(ele);
  32. maxele=ele;
  33.  
  34.  
  35. }
  36. else if(min>=hash.get(ele))
  37. {
  38. min=hash.get(ele);
  39. minele=ele;
  40. }
  41. }
  42.  
  43.  
  44. System.out.println("the ele with max freq is "+maxele+" with freq= "+max);
  45. System.out.println("the ele with min freq is "+minele+" with freq= "+min);
  46. }
  47. }
Success #stdin #stdout 0.23s 60908KB
stdin
6
1 2 2 9 0 0 
stdout
the ele with max freq is 0 with freq= 2
the ele with min freq is 9 with freq= 1