fork download
  1. /* package whatever; // don't place package name! */
  2. //Finding min and max freq in an array
  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.  
  13.  
  14. int arr[] = {3,2,3,2,4,3};
  15. int n = arr.length;
  16.  
  17. int maxElem=0,minElem=0;
  18. int maxFreq=0,minFreq= Integer.MAX_VALUE;
  19.  
  20. for(int i = 0;i<n;i++){
  21. int count = 0;
  22. for(int j = 0;j<n;j++){
  23. if(arr[i] == arr[j]){
  24. count++;
  25. }
  26. }
  27. if(maxFreq < count){
  28. maxFreq = count;
  29. maxElem = arr[i];
  30. }
  31. if(minFreq > count){
  32. minFreq = count;
  33. minElem = arr[i];
  34. }
  35. }
  36.  
  37. System.out.println(maxElem + " : " + maxFreq);
  38. System.out.println(minElem + " : " + minFreq);
  39. }
  40. }
Success #stdin #stdout 0.15s 55464KB
stdin
Standard input is empty
stdout
3 : 3
4 : 1