fork download
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. int n = 6;
  7. int[] arr = {1, 1, 2, 3, 3, 3};
  8.  
  9. Map<Integer, Integer> mp = new HashMap<>();
  10. int maxiFreq = Integer.MIN_VALUE, maxiElement = arr[0];
  11. int miniFreq = Integer.MAX_VALUE, miniElement = arr[0];
  12.  
  13. for (int i = 0; i < arr.length; i++) {
  14. mp.put(arr[i], mp.getOrDefault(arr[i], 0) + 1);
  15. }
  16.  
  17. for (Map.Entry<Integer, Integer> num : mp.entrySet()){
  18. if (num.getValue() >= maxiFreq){
  19. maxiFreq = num.getValue();
  20. maxiElement = num.getKey();
  21. }
  22. if (num.getValue() <= miniFreq){
  23. miniFreq = num.getValue();
  24. miniElement = num.getKey();
  25. }
  26. }
  27.  
  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. }
  34.  
Success #stdin #stdout 0.12s 57544KB
stdin
5
1 5 3 2 8
stdout
Max frequency element: 3 with frequency: 3
Min frequency element: 2 with frequency: 1