/**
* @author lautturi.com
* Java example: java count duplicates in int array
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
int[] numbers = { 11,4,12,7,55,8,12,8,13,55,12,7 };
Arrays.sort(numbers);
int previous = numbers[0] - 1;
int dupCount = 0;
for (int i = 0; i < numbers.length; ++i) {
if (numbers[i] == previous) {
++dupCount;
} else {
previous = numbers[i];
}
}
System.out.println("There were " + dupCount + " duplicates in the array.");
}
}Sourcwww:e.lautturi.comoutput:
There were 5 duplicates in the array.