Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. public class MaxRepeatingElement {
  2. public void MaxRepeatingElementInPlace(int [] arrA){
  3. int size = arrA.length;
  4. int maxCount=0;
  5. int maxIndex=0;
  6. for (int i = 0; i <size ; i++) {
  7. //get the index to be updated
  8. int index = arrA[i]% size;
  9. arrA[index] = arrA[index] + size;
  10. }
  11. for (int i = 0; i <size ; i++) {
  12. if(arrA[i]/size>maxCount){
  13. maxCount=arrA[i]/size;
  14. maxIndex=i;
  15. }
  16. }
  17. System.out.println("Element repeating maximum no of times: " + arrA[maxIndex]%size + ", maximum count: " + maxCount);
  18. }
  19. public static void main(String[] args) {
  20. int [] arrA = {4, 1, 5, 2, 1, 5, 9, 8, 6, 5, 3, 2, 4, 7};
  21. MaxRepeatingElement m = new MaxRepeatingElement();
  22. m.MaxRepeatingElementInPlace(arrA);
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement