Last active
January 28, 2022 16:28
-
-
Save thmain/1cea9f46da8b345e71bff559fea24980 to your computer and use it in GitHub Desktop.
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 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