Skip to content

Instantly share code, notes, and snippets.

@vignarajj
Created February 13, 2019 04:29
Show Gist options
  • Save vignarajj/af961eaa55926e51b321397eedcbbf4f to your computer and use it in GitHub Desktop.
Save vignarajj/af961eaa55926e51b321397eedcbbf4f to your computer and use it in GitHub Desktop.
Find the integer appearing most time in the array
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