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. int arr[] = {3,2,3,2,4,3};
  14. int n = arr.length;
  15.  
  16. int maxElem = 0,minElem = 0;
  17. int minFreq = Integer.MAX_VALUE;
  18. int maxFreq = 0;
  19.  
  20. Map<Integer,Integer> mp = new HashMap<>();
  21.  
  22. for(int i =0;i<n;i++){
  23. mp.put(arr[i],mp.getOrDefault(arr[i],0) + 1);
  24. }
  25.  
  26. for(Map.Entry<Integer,Integer> num : mp.entrySet()){
  27. if(num.getValue() > maxFreq){
  28. maxFreq = num.getValue();
  29. maxElem = num.getKey();
  30. }
  31.  
  32. if(num.getValue()< minFreq){
  33. minFreq = num.getValue();
  34. minElem = num.getKey();
  35. }
  36. }
  37.  
  38. System.out.println(maxElem +" : " + maxFreq);
  39. System.out.println(minElem + " : " + minFreq);
  40. }
  41. }
Success #stdin #stdout 0.19s 55504KB
stdin
Standard input is empty
stdout
3 : 3
4 : 1