fork download
  1. import java.util.*;
  2.  
  3. class Ideone {
  4. public static void main(String[] args) throws java.lang.Exception {
  5. Scanner sc = new Scanner(System.in);
  6. int n = sc.nextInt();
  7. int arr[] = new int[n];
  8.  
  9.  
  10. for(int i = 0; i < n; i++) {
  11. arr[i] = sc.nextInt();
  12. }
  13.  
  14.  
  15. HashMap<Integer, Integer> hm = new HashMap<>();
  16. for(int i = 0; i < n; i++) {
  17. hm.put(arr[i], hm.getOrDefault(arr[i], 0) + 1);
  18. }
  19.  
  20. int maxCount = Integer.MIN_VALUE;
  21. int minCount = Integer.MAX_VALUE;
  22. int maxValue = 0;
  23. int minValue = 0;
  24.  
  25.  
  26. for(Integer key : hm.keySet()) {
  27. int count = hm.get(key);
  28. if(maxCount < count) {
  29. maxCount = count;
  30. maxValue = key;
  31. }
  32. if(minCount > count) {
  33. minCount = count;
  34. minValue = key;
  35. }
  36. }
  37.  
  38.  
  39. System.out.println("Minimum Frequency: " + minCount + " (Value: " + minValue + ")");
  40. System.out.println("Maximum Frequency: " + maxCount + " (Value: " + maxValue + ")");
  41. }
  42. }
  43.  
Success #stdin #stdout 0.26s 58800KB
stdin
8
3
3
8
3
9
9
9
9
stdout
Minimum Frequency: 1 (Value: 8)
Maximum Frequency: 4 (Value: 9)