# Find Duplicate Elements in an Array – Java Program

Write a java program to find duplicate elements in an array. There are multiple ways through which we can find duplicate elements in an array. In this tutorial, I’ll discuss three ways to find duplicate elements in an array.
This question is also very important in terms of technical interviews.

Find Duplicate Elements in an Array – Java Program

## How to Find Duplicate Elements in an Array – Java Program Examples

METHOD 1 – Using brute force approach

This is the most basic and easiest approach to search and print duplicate elements of an array. In this approach, we use two for loops (inner and outer loops) to compare an element with each element of an array. The time complexity of this approach is O(n2).

This approach is not suitable for large data sets.

Subscribe Our Tutorials

## Find Duplicate Elements in an Array using Java HashMap

This approach is much better than the brute force approach. In this approach, we take the advantage of very important data structure called Hash Table.

Java program to find first non-repeated character in a string

Traverse an array and insert an element and it’s count (no. of occurrences). Then, Traverse a map to check all the keys whose value is greater than 1. Those keys whose value is greater than 1 are duplicate elements in an array. The Time complexity of this approach is O(n).

## Java HashSet to Find Duplicate Elements in an Array

In computer science, A set is a data structure which doesn’t allow duplicates. In java, we can use HashSet class to solve this problem. Traverse an array and insert an element into HashSet using the add() method. If the add() method returns false, it means that element is duplicate value.

The time complexity of this approach is O(n).

### Conclusion

I have explained three methods to solve this problem. If you know some other approach to solve this problem, then you can let us know through your comments.