Created
February 13, 2019 04:29
-
-
Save vignarajj/af961eaa55926e51b321397eedcbbf4f to your computer and use it in GitHub Desktop.
Find the integer appearing most time in the array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MostAppear { | |
public static void main(String[] args) { | |
int[] arr = {1, 2, 3, 4, 1, 6, 1, 2, 2, 2, 2, 4, 6}; | |
System.out.println("Max appearance number is " + maxAppearance(arr)); | |
} | |
public static int maxAppearance(int[] arr) { | |
int len = arr.length; | |
int max_count = 1; | |
int app_int = arr[0]; | |
int current_count = 1; | |
for (int i = 1; i < len; i++) { | |
if (arr[i] == arr[i - 1]) { | |
current_count++; | |
} else { | |
if (current_count > max_count) { | |
max_count = current_count; | |
app_int = arr[i - 1]; | |
} | |
current_count = 1; | |
} | |
} | |
return app_int; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment