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. HashMap<Integer, Integer> map = new HashMap<>();
  15.  
  16. int n = sc.nextInt();
  17. int[] arr = new int[n];
  18.  
  19. for (int i = 0; i < n; i++) {
  20. arr[i] = sc.nextInt();
  21. map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
  22. }
  23.  
  24. int max_freq = Integer.MIN_VALUE;
  25. int min_freq = Integer.MAX_VALUE;
  26. int max_num = -1;
  27. int min_num = -1;
  28.  
  29. for(HashMap.Entry<Integer,Integer> idx : map.entrySet()){
  30. int num = idx.getKey();
  31. int count = idx.getValue();
  32.  
  33. if(count>max_freq){
  34. max_freq = count;
  35. max_num = num;
  36. }
  37.  
  38. if(count<min_freq){
  39. min_freq =count;
  40. min_num = num;
  41. }
  42. }
  43. System.out.println("max number is " + max_num + " => " + max_freq);
  44. System.out.println("min number is " + min_num + " => " + min_freq);
  45.  
  46.  
  47.  
  48.  
  49. }
  50. }
Success #stdin #stdout 0.16s 60952KB
stdin
10
1 1 1 2 2 2 4 3 3 1
stdout
max number is 1 => 4
min number is 4 => 1