Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. int[] array = new int[10];
  2.  
  3. Scanner s = new Scanner(System.in);
  4. System.out.print("Enter numbers between 1 and 10");
  5.  
  6. for (int i = 0; i < array.length; i++) {
  7. System.out.print("Number " + (i+1) + ": ");
  8. array[i] = s.nextInt();
  9. }
  10. System.out.println("Most popular element: " + findMostPopularElement(array));
  11. }
  12.  
  13.  
  14. public static int findMostPopularElement(int[] array){
  15. int mostUsedElementCount = 0;
  16. int elementCount = 0;
  17. int searchElement =0;
  18.  
  19. for (int i = 0; i < array.length; i++) {
  20.  
  21. for (int j = 0; j < array.length; j++) {
  22.  
  23. if (array[i]==array[j]) {
  24. elementCount++;
  25. if (mostUsedElementCount<elementCount ) {
  26. mostUsedElementCount=elementCount;
  27. searchElement = array[i];
  28.  
  29. }
  30. }
  31. }
  32. elementCount=0;
  33. }
  34.  
  35.  
  36.  
  37. return searchElement;
  38.  
  39.  
  40.  
  41. }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement