### Java Program to Find Duplicate Elements in an Array

Arrays are a fundamental data structure in programming. They are used to store a collection of items (which can be of the same type). However, at times, we might end up with duplicate items in an array, either intentionally or due to oversight. Let's see how to write a Java program to identify these duplicates from an Array.

# The Strategy

• We'll loop through the array to pick each element.
• For every picked element, we'll loop again to compare it with the rest.
• If any two elements match, we've found a duplicate.

# Java Program to Find Duplicate Elements in an Array

``````public class DuplicateFinder {

public static void main(String[] args) {
int[] arr = {1, 3, 5, 2, 4, 5, 3};

System.out.println("Duplicate elements in the array are:");
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
System.out.print(arr[i] + " ");
}
}
}
}
}``````

## Output:

``Duplicate elements in the array are:3 5 ``

## Step by Step Explanation:

Setting Up the Array:
``        int[] arr = {1, 3, 5, 2, 4, 5, 3};``
We've initialized an integer array arr containing 7 elements. You can notice the numbers 5 and 3 appear more than once in this array.
Nested Loops to Compare Elements:
``````        for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {``````
The outer loop starts from the first element (index 0). For every element selected by the outer loop, the inner loop compares it with all the elements after it. The inner loop's index j starts from i + 1 to ensure each pair of elements is compared only once.

Checking for Duplicates:
``````                if (arr[i] == arr[j]) {
System.out.print(arr[i] + " ");
}``````
If we find any two elements that match, we've found a duplicate, and we print it.

## Note:

While this approach is simple and perfect for beginners, it's not the most efficient, especially for large arrays. The nested loop means that the program has a time complexity of O(n^2). As you advance in Java, you'll learn more optimized techniques using data structures like HashSet is to handle such problems more efficiently.

# Optimized Strategy

• Traverse through the array elements.
• For each element, try inserting it into the Set.
• If the insertion fails (because the element already exists in the set), it's a duplicate.

## Optimized Java Program:

``````import java.util.HashSet;
import java.util.Set;

public class OptimizedDuplicateFinder {

public static void main(String[] args) {
int[] arr = {1, 3, 5, 2, 4, 5, 3};

System.out.println("Duplicate elements in the array are:");
findDuplicates(arr);
}

public static void findDuplicates(int[] arr) {
Set<Integer> set = new HashSet<>();

for (int value : arr) {
System.out.print(value + " ");
}
}
}
}``````

## Output:

``Duplicate elements in the array are:5 3 ``

## Explanation:

HashSet Initialization:
``        Set<Integer> set = new HashSet<>();``
We use HashSet, a widely-used implementation of the Set interface. It uses a hash table to store elements, which provides constant time performance for basic operations like add, remove, and contains.

Traversing Through the Array:
``        for (int value : arr) {``
This is a for-each loop that iterates over each element of the arr.

Adding to the Set & Checking for Duplicates:
``````            if (!set.add(value)) {
System.out.print(value + " ");
}``````
The add method attempts to add the element to the set. If the element already exists in the set, the method returns false, indicating a duplicate. We then print out the duplicate value.

# Conclusion

In this article, we learned how to write a Java program to find the duplicate elements from an Array. We also seen how to optimize the program using HashSet. Detecting duplicates using a HashSet is an optimized and efficient way, especially when dealing with large arrays. This solution showcases the importance of the Java Collections Framework in solving real-world problems efficiently.