Hey I've been messing around in java and created this program that will go through an array and find all duplicates in the array and its working which is great! but there is one small problem if I run the program and there is more than 2 duplicates of one value it will output two duplicates found for the same value - this will probably explain it better
Output
Duplicates in Array: 1
Duplicates in Array: 8
Duplicates in Array: 8
I've been stuck on this for some time now I know its probably some small error but if anyone could help me out that would be awesome, the code is below, thanks
import java.util.Arrays;
public class Duplicates {
public static void main (String[] args) {
int[] values = { 8, 5, 9, 8, 6, 13, 33, 1, 98, 12, 8, 1 };
Arrays.sort(values);
for(int i = 1; i < values.length; i++) {
if(values[i] == values[i - 1]) {
System.out.println("Duplicates in Array: " + values[i]);
}
}
}
}