Program to Print the Duplicate Elements of an Array | Find Duplicate Elements in an Array in Java

20 Jan 2025 | 6 min read

Finding duplicate elements in an array is a commonplace programming hassle that assesses the know-how of statistics systems and algorithms. Understanding the different methods to find duplicates efficiently is essential to creating custom solutions for applications such as data analytics. In database management or web development, it is important to check the accuracy of the data and optimize it.

Approaches to Finding Duplicates in an Array

1. Brute Force Method

The brute-force method checks each element against other elements in the array. It has a time complexity of (O(n^2)) wherein (n) is the duration of the array. This method is not always very suitable because it's far inefficient, especially for large arrays.

File Name: DuplicateBruteForce.java

Output:

Duplicate element found: 5
Duplicate element found: 6

Explanation

The brute-force method involves iterating to compare each element of the array with every other element. The outer loop specifies one element. The inner circle will be tested with all subsequent elements. If matching items are found, elements will be marked as duplicates. This method is simple but inefficient. It has a time complexity of (O(n^2)), which makes it impractical for large arrays.

2. Using Sorting

The first sort method sorts the array. Then it checks for duplicate elements accordingly. Classification (O(n log n)) and weeding through the dam maintenance matrix (O(n)) make this method significantly more effective than brute force.

File Name: DuplicateSorting.java

Output:

Duplicate element found: 5
Duplicate element found: 6

Explanation

This method sorts the array first. Then iterate to see if two consecutive elements are the same. Sorting an array takes time (O(n log n)) and the resulting linear scan increases complexity (O(n)). This method is more efficient than brute force. But it modifies the original array until it is copied.

3. Using HashSet

The HashSet method is one of the most efficient ways to find duplicates in an array. A HashSet allows unique elements. So, whilst we attempt to add a detail that already exists within the set, we will realise that the factors are duplicates.

File Name: DuplicateHashSet.java

Output:

Duplicate element found: 5
Duplicate element found: 6

Explanation

The HashSet approach for finding duplicates is to iterate through the array and attempt to add every element to the HashSet because HashSet simplest allows unique values. If we strive to add an element that already exists inside the set It will return false.

Each element from the array is tested by adding it to the HashSet. If add() returns false, then it indicates the existence of duplicate elements in the array. The time complexity of this approach is average (O(n)) because each addition operation to a HashSet takes (O()) time.

4. Using HashMap

Using a HashMap allows us to count occurrences of each element. We iterate over the array and add each element to the HashMap, updating the count as we go. Elements with a count greater than are duplicates.

File Name: DuplicateHashMap.java

Output:

Duplicate element found: 5
Duplicate element found: 6

Explanation

In this approach, a HashMap is used to count occurrences of each element. As each element is processed, its count is updated in the HashMap. After processing, any element with a count greater than one is identified as a duplicate. This method has a time complexity of (O(n)) and a space complexity of (O(n)), making it suitable for scenarios where frequency counts are also needed.

5. Using Frequency Array

For arrays with a limited range of values We can use frequency arrays to keep track of the number of each element. This method works only if we know the maximum possible value in the array.

File Name: DuplicateFrequencyArray.java

Output:

Duplicate element found: 5
Duplicate element found: 6

Explanation

This approach uses an auxiliary frequency array to count the occurrences of each element, assuming the array values are within a known range. Each index in the frequency array represents an element, and its value represents the count of that element in the input array. This method has time complexity (O(n)) and space complexity (O(range), making it efficient but limited by range limitations.

6. Using Floyd's Tortoise and Hare Algorithm

Suppose the array is established to acquire cycles due to redundancy. In that case, If a duplicate is discovered (such as in a related list), Floyd's Tortoise and Hare algorithm may be able to find duplicates.

File Name: DuplicateCycleDetection.java

Output:

Duplicate element found: 2

Explanation

This algorithm uses two pointers moving at different speeds to detect a cycle in the array, which occurs when duplicates exist under certain constraints (for example, elements act as pointers within a limited range). Once a cycle is detected, the duplicate is located by resetting one pointer and advancing both pointers at the same speed until they meet. This method works in (O(n)) time with (O()) space, making it efficient when applicable.

Choosing the Optimal Solution

The choice of algorithm depends on:

  1. Time Complexity: If you need a solution with minimal time complexity, a HashSet or HashMap approach is often optimal, as both offer an average time complexity of (O(n)).
  2. Memory Constraints: If memory is limited, the sorting-based approach might be favourable since it requires no additional memory if sorting is done in place.
  3. Input constraints: If the array contains values within a known range. Frequency array or even Floyd's Tortoise and Hare algorithm might be the best solution.

Conclusion

Detecting duplicates in an array is a fundamental problem that has several solutions. Each method is suitable for different situations. Brute-force methods, although simple, it is inefficient for large data sets and can best be avoided. Sorting is a good choice. But it modifies the array and still has more time complexity compared to other methods.

Hash-based methods, such as using HashSet or HashMap, are very efficient with a time complexity of (O(n)), making them suitable for most use cases. Although it requires additional space, the frequency array method will work... When the array is limited in range, it cannot handle the required input values efficiently.

Floyd's Tortoise and Hare algorithm provides a unique and space-saving solution to a recurring problem. The selection of the most appropriate method depends on constraints such as time, location, and nature of the input data. Understanding these techniques helps developers create robust and efficient programs for non-real-world applications.


Next TopicJava Programs