Java int array find duplicate elements example shows how to find duplicate elements in an int array. The example also shows how to count duplicate elements in various ways.
How to find duplicate elements in an int array?
There are various approaches to find duplicate elements in the array as given below.
1) Java int array find duplicate elements using a boolean array
We can use a boolean array of the size equal to the maximum possible value of any element of int array as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int intArray[] = {1, 2, 3, 2, 5, 6, 5}; int count = 0; boolean[] maxValues = new boolean[1000]; for(int i = 0; i < intArray.length; i++){ if( maxValues[intArray[i]] ){ System.out.println("Duplicate element: " + intArray[i] + ", at index " + i); count++; } maxValues[intArray[i]] = true; } System.out.println("Duplicate element count:" + count); |
Output
1 2 3 |
Duplicate element: 2, at index 3 Duplicate element: 5, at index 6 Duplicate element count:2 |
Here, the maxValues array must be of a size equal to the maximum possible value of any element of the int array. We first created the boolean array. The elements of the boolean array are automatically initialized to a false value. Then we looped through the int array and checked if the element of the boolean array at that index contains the true value. If it has true value then that means the same element was encountered previously which set the element to true from false value. In the end, we make an element of a boolean value at int array value index to true.
Note: This approach is fast, but if the integer array contains large int values, then the corresponding boolean array will take a lot of memory. This approach should be used only when the maximum possible element value is not too large.
2) Using for loop
You can use two for loops to compare each element of an array with every other element to find duplicate elements as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int intArray[] = {1, 2, 3, 2, 5, 6, 5}; int count =0; for(int i = 0 ; i < intArray.length; i++){ for(int j = i + 1; j < intArray.length; j++){ if(intArray[i] == intArray[j]){ System.out.println("Duplicate element: " + intArray[j] + ", at index " + j); count++; } } } System.out.println("Duplicate element count:" + count); |
Output
1 2 3 |
Duplicate element: 2, at index 3 Duplicate element: 5, at index 6 Duplicate element count:2 |
This approach compares each element of an array to every other element of the same array. If any match is found, it means that the array contains duplicate elements.
Note: This approach should only be used for arrays less in size as it loops through every element and compares them with every other element.
3) Using the HashSet class
The HashSet class allows only unique values. You can use that property of the HashSet to check for duplicate elements in the array as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
int intArray[] = {1, 2, 3, 2, 5, 6, 5}; Set<Integer> uniqueValues = new HashSet<Integer>(); int count = 0; for(int i = 0 ; i < intArray.length; i++){ if(uniqueValues.contains(intArray[i])){ System.out.println("Duplicate element: " + intArray[i] + ", at index " + i); count++; } uniqueValues.add(intArray[i]); } System.out.println("Duplicate element count:" + count); |
For each element of an array, we check if the element exists in the HashSet. If an element exists, it means that we previously added it to the HashSet so it is duplicate. At the end of the loop, we add element to the HashSet.
Alternatively, the add
method of HashSet returns a boolean value depending upon whether the element was added to it or not. If the element is not added because the HashSet already contains the element, then the add
method returns false. We can use that to find the duplicate element in the array as given below.
1 2 3 4 5 6 7 8 9 10 |
int intArray[] = {1, 2, 3, 2, 5, 6, 5}; Set<Integer> uniqueValues = new HashSet<Integer>(); for(int i = 0 ; i < intArray.length; i++){ if( !uniqueValues.add(intArray[i]) ) System.out.println("Duplicate element: " + intArray[i] + ", at index " + i); } |
Output
1 2 |
Duplicate element: 2, at index 3 Duplicate element: 5, at index 6 |
4) Using for loop and sort method of Arrays class
You can sort the int array first using sort
method of the Arrays class and then check if any of the two consecutive elements are the same to find the duplicate elements in an array as given below.
1 2 3 4 5 6 7 |
int intArray[] = {1, 2, 3, 2, 5, 6, 5}; Arrays.sort(intArray); for(int i = 0 ; i < intArray.length-1; i++){ if( intArray[i] == intArray[i+1] ) System.out.println("Duplicate element: " + intArray[i] + ", at index " + (i+1)); } |
Output
1 2 |
Duplicate element: 2, at index 2 Duplicate element: 5, at index 5 |
We sorted the array first, so if the array contains the duplicate elements they will be ordered right next to each other. We then looped through the sorted array to see if any of the two consecutive elements are the same to find the duplicate values.
Note: This approach requires the extra overhead of sorting the array first. It also modifies the original array by sorting it. Use this approach only if the array is size is small to moderate and you don’t mind changing the original array.
This example is a part of the Java Array tutorial with examples.
Please let us know your views in the comments section below.