Skip to content

Java int array find duplicate elements example

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.

Output

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.

Output

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.

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.

Output

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.

Output

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.

About the author

Leave a Reply

Your email address will not be published.