fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6.  
  7. int n = sc.nextInt();
  8.  
  9. HashMap<Integer, Integer> k = new HashMap<>();
  10.  
  11. for(int i = 0; i < n; i++){
  12. int y = sc.nextInt();
  13. //while taking the input itself putting into the map
  14. // so automatically increments the freq
  15. k.put(y, k.getOrDefault(y, 0) + 1);
  16. }
  17. //taking the max value for minFreq so that when we get min value we update it
  18. int minFrequency= Integer.MAX_VALUE, maxFrequency = 0;
  19. int minElement = -1, maxElement = -1;
  20.  
  21. for(Map.Entry<Integer, Integer> itr : k.entrySet()){
  22. int number = itr.getKey();
  23. int count = itr.getValue();
  24.  
  25. if(count < minFrequency){
  26. minFrequency = count;
  27. minElement = number;
  28. }
  29.  
  30. if(count > maxFrequency ){
  31. maxFrequency = count;
  32. maxElement = number;
  33. }
  34. }
  35.  
  36. System.out.println("minimum freq having Element is "+ " " + minFrequency );
  37. System.out.println("maximum freq having Element is "+ " " + maxFrequency);
  38. }
  39. }
Success #stdin #stdout 0.13s 58932KB
stdin
3
1 1 2
stdout
minimum freq having Element is  1
maximum freq having Element is  2