Can someone help me with this java code i'm trying to write for my programming class. I'm supposed to write a method that takes in an array of integers and counts how many different numbers appear more than once.
Example given is for the input { 1; 1; 1; 2; 4; 4; 5; 6; 6 } the answer would be 3 because three different numbers (1,4,6) appear more than once.
The code I've written so far is:
Code:
public static int checkRepetition(int[] a)
{
int repetitionCount = 0;
for(int i=0; i<a.length; i++)
{
for(int j=0; j<i; j++)
{
if(a[i] == a[j])
{
repetitionCount++;
}
}
}
return repetitionCount;
}
As you can see what I'm doing here is for every element in a[] I'm checking if any previous element has the same value as a[i].
What this does though is counts every repetition so that if any same integer appears 3 times in the array then it will be counted as two repetitions, which isn't what I want to do, since I only wanna count how many integers appear more than once (doesn't matter if an integer appears twice, three times, ... it should still only add 1 to my repetitionCount.
Hope the question is clear and if this isn't the right thread for this I apologize