I've created a method in java that checks (and counts) for duplicate values in an array:
private static void checkDuplicates(String[] array)
{
int arrayLength = array.length;
for (int i = 0; i < arrayLength; i++) {
int count = 0;
for (int i2 = 0; i2 < arrayLength; i2++) {
if (array[i].equals(array[i2])) {
count++;
}
}
System.out.println("The value " + array[i] + " appears " + count + " times in the array.");
}
}
Given the array {string1, string1, string2, string3, string1}, this produces the following output:
The value string1 appears 3 times in the array.
The value string1 appears 3 times in the array.
The value string2 appears 1 times in the array.
The value string3 appears 1 times in the array.
The value string1 appears 3 times in the array.
As you've probably understood by now, I don't want the program to print one line for each occurence of the duplicate array element.
I'm sure this problem has an easy solution, but I've been working at this for hours and I can't figure it out. Could someone point me in the right direction?