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. int n[] = { 6, 4, 6, 4, 6, 4, 1 };
  13. List<Integer> maxNums = new ArrayList<Integer>();
  14.  
  15. int max = Integer.MIN_VALUE;
  16. Integer lastValue = null;
  17. int currentCount = 0;
  18. Arrays.sort(n);
  19. for( int i : n ){
  20. if( lastValue == null || i != lastValue ){
  21. if( currentCount == max ){
  22. maxNums.add(lastValue);
  23. }
  24. else if( currentCount > max ){
  25. maxNums.clear();
  26. maxNums.add(lastValue);
  27. max = currentCount;
  28. }
  29. lastValue = i;
  30. currentCount = 1;
  31. }
  32. else {
  33. currentCount++;
  34. }
  35. System.out.println("i=" + i + ", currentCount=" + currentCount);
  36. }
  37. if( currentCount == max ){
  38. maxNums.add(lastValue);
  39. }
  40. else if( currentCount >= max ){
  41. maxNums.clear();
  42. maxNums.add(lastValue);
  43. }
  44.  
  45. System.out.println(maxNums);
  46. }
  47. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
i=1, currentCount=1
i=4, currentCount=1
i=4, currentCount=2
i=4, currentCount=3
i=6, currentCount=1
i=6, currentCount=2
i=6, currentCount=3
[4, 6]