I am trying to count the number of occurrences in an array of integers and return the amount of times each number was displayed. My code counts the right amount of occurrences, but it displays them more than once.
Here is the relevant code:
int num = input.nextInt();
int count = 0;
int[] integers = new int[num];
for(int i = 0; i < num; i++) {
System.out.print("Enter int 1-50: ");
integers[i] = input.nextInt();
}
for(int i = 0; i < num; i++) {
for(int j = 0; j < num; j++) {
if(integers[i] == integers[j]) {
count++;
}
}
System.out.println(integers[i] + " occurs "+count+" times.");
count = 0;
}
The issue is that if a number occurs more than once, it displays that number more than once. For example, {1, 2, 2, 3} would print "2 occurs 2 times" twice. I understand why this happens but I'm wondering if there's a simple way to make sure these statements only print once.