fork download
  1. class Ideone {
  2. public static void main (String[] args) {
  3. System.out.println(modeCalc(1, 2, 3, 3, 4, 4, 4, 5)); // 4
  4. System.out.println(modeCalc(1, 2, 2, 2, 4, 4, 4, 5)); // 2 (or 4 but it finds the first one)
  5. }
  6. public static double modeCalc(double... numRead) {
  7. double maxValue = Double.NaN;
  8. int maxCount = 0, count = 0;
  9. for (int i = 0; i < numRead.length; i++) {
  10. if (i > 0 && numRead[i] != numRead[i - 1])
  11. count = 0;
  12. if (++count > maxCount) {
  13. maxValue = numRead[i];
  14. maxCount = count;
  15. }
  16. }
  17. return maxValue;
  18. }
  19. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
4.0
2.0