Java Basic Examples

Java int array find duplicate elements example

Java int array find duplicate elements example shows how to find duplicate elements in int array. Example also shows how to find duplicate, count duplicate elements in various ways.

How to find duplicate elements in int array?

There are various approaches to find duplicate elements in array as given below.

1) Java int array find duplicate elements using boolean array

You can use boolean array of size equal to the maximum possible value of any element of int array as given below.

Output

Here, maxValues array must be of size equal to the maximum possible value of any element of the int array. We first created the boolean array. Elements of boolean array are automatically initialized to false value. Then we looped through the int array and checked if the element of boolean array at that index contains true value. If it has true value then that means same element was encountered previously which set the element to true from false value. At the end we make element of boolean value at int array value index to true.

Note: This approach is fast, but if the integer array contains large int values, then 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) Java int array find duplicate elements using for loop

You can use two for loops to compare each element of an array with every other elements to find duplicate element as given below.

Output

This approach compares each element of an array to every other elements of same array. If any match is found, array contains duplicate.

Note: This approach should only be used for arrays less in size as it loops through every element and compares them with every other elements.

3) Java int array find duplicate elements using HashSet

HashSet allows only unique values. You can use that property of the HashSet to check for duplicate elements in array as given below.

For each element of an array, we check if the element exists in the HashSet. If 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, add method of HashSet returns boolean value depending upon whether the element was added to it or not. If element is not added because the HashSet already contains the element, then add method returns false. We can use that to find the duplicate element in array as given below.

Output

4) Java int array find duplicate elements using for loop and sort method of Arrays class

You can sort the int array first using sort method of Arrays class and then check if any of the two consecutive elements are same to find duplicate element in an array as given below.

Output

We sorted the array first, so if array contained the duplicate elements they will be ordered right next to each other. We than looped through the sorted array to see if any of the two consecutive elements are same to find the duplicate values.

Note: This approach requires extra overhead of sorting the array first. It also modifies the original array by sorting it.

Please let us know your views in the comments section below.

 

Tags
Join 1000+ fellow learners! Enter your email address below: