
Write a program that read 15 integers between 0 and 6 and a method to search, displays and returns the number that has the highest occurrence. Prompt the user if the number is not within this range.

Hello Friend,
Try the following code:
import java.util.*;
public class NumberExample {
public static void searchAndDisplay(int[] arr) {
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
for (int element : arr) {
int index = list1.indexOf(element);
if (index != -1) {
int newCount = list2.get(index) + 1;
list2.set(index, newCount);
} else {
list1.add(element);
list2.add(1);
}
}
int maxCount = 0;
int index = -1;
for (int i = 0; i < list2.size(); i++) {
if (maxCount < list2.get(i)) {
maxCount = list2.get(i);
index = i;
}
}
System.out.println("Number " + list1.get(index)+ " has highest occurrence i.e " + maxCount);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] arr = new int[15];
System.out.println("Enter 15 integers (0-6):");
for (int i = 0; i < arr.length; i++) {
int num = input.nextInt();
if (num <= 6 && num >= 0) {
arr[i] = num;
} else {
System.out.print("Enter another number: ");
num = input.nextInt();
}
}
searchAndDisplay(arr);
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.