Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active January 28, 2022 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/1cea9f46da8b345e71bff559fea24980 to your computer and use it in GitHub Desktop.
Save thmain/1cea9f46da8b345e71bff559fea24980 to your computer and use it in GitHub Desktop.
public class MaxRepeatingElement {
public void MaxRepeatingElementInPlace(int [] arrA){
int size = arrA.length;
int maxCount=0;
int maxIndex=0;
for (int i = 0; i <size ; i++) {
//get the index to be updated
int index = arrA[i]% size;
arrA[index] = arrA[index] + size;
}
for (int i = 0; i <size ; i++) {
if(arrA[i]/size>maxCount){
maxCount=arrA[i]/size;
maxIndex=i;
}
}
System.out.println("Element repeating maximum no of times: " + maxIndex + ", maximum count: " + maxCount);
}
public static void main(String[] args) {
int [] arrA = {4, 1, 5, 2, 1, 5, 9, 8, 6, 5, 3, 2, 4, 7};
MaxRepeatingElement m = new MaxRepeatingElement();
m.MaxRepeatingElementInPlace(arrA);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment