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 = {1,2,3,4,4,5,5,5};
  14. HashMap<Integer, Integer> mpp = new HashMap<>();
  15. for(int i=0;i<arr.length;i++){
  16. mpp.put(arr[i],mpp.getOrDefault(arr[i],0)+1);
  17. }
  18. int maxiFreq = Integer.MIN_VALUE, maxiElement = arr[0];
  19. int miniFreq = Integer.MAX_VALUE, miniElement = arr[0];
  20. for(Map.Entry<Integer, Integer> e: mpp.entrySet()){
  21. if (e.getValue() >= maxiFreq){
  22. maxiFreq = e.getValue();
  23. maxiElement = e.getKey();
  24. }
  25. if (e.getValue() <= miniFreq){
  26. miniFreq = e.getValue();
  27. miniElement = e.getKey();
  28. }
  29. }
  30. System.out.println("Max frequency element: " + maxiElement + " with frequency: " + maxiFreq);
  31. System.out.println("Min frequency element: " + miniElement + " with frequency: " + miniFreq);
  32. }
  33. }
Success #stdin #stdout 0.18s 55500KB
stdin
Standard input is empty
stdout
Max frequency element: 5 with frequency: 3
Min frequency element: 3 with frequency: 1