Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int[] array = new int[10];
- Scanner s = new Scanner(System.in);
- System.out.print("Enter numbers between 1 and 10");
- for (int i = 0; i < array.length; i++) {
- System.out.print("Number " + (i+1) + ": ");
- array[i] = s.nextInt();
- }
- System.out.println("Most popular element: " + findMostPopularElement(array));
- }
- public static int findMostPopularElement(int[] array){
- int mostUsedElementCount = 0;
- int elementCount = 0;
- int searchElement =0;
- for (int i = 0; i < array.length; i++) {
- for (int j = 0; j < array.length; j++) {
- if (array[i]==array[j]) {
- elementCount++;
- if (mostUsedElementCount<elementCount ) {
- mostUsedElementCount=elementCount;
- searchElement = array[i];
- }
- }
- }
- elementCount=0;
- }
- return searchElement;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement