Advertisement
ultravibez

Most Common Number

Sep 3rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.43 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.                 mostCommon = arr[i];
  9.             } else
  10.                 counter = tempCounter > counter ? tempCounter : counter;
  11.         }
  12.         return mostCommon;
  13.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement