Advertisement
ultravibez

Fixed mostCommonNum

Sep 4th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.55 KB | None | 0 0
  1.     public static int mostCommonNum(int[] arr) {
  2.         int mostCommon = arr[0];
  3.         int counter = 0;
  4.         int tempCounter = 0;
  5.         for (int i = 0; i < arr.length - 1; i++) {
  6.             if (arr[i] == arr[i + 1]) {
  7.                 counter++;
  8.             }
  9.             else if (arr[i] < arr[i + 1]) {
  10.                 if (tempCounter < counter) {
  11.                     tempCounter = counter;
  12.                     mostCommon = arr[i];
  13.                 }
  14.                 counter = 0;
  15.             }
  16.         }
  17.         return mostCommon;
  18.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement