Table of contents
1.
Introduction
2.
What is duplicate elements in array?
3.
Why Identifying Duplicates Matters in Programming
4.
How to find Duplicates Elements in Array?
4.1.
Code
4.2.
Java
4.3.
Output
5.
Remove Duplicates Element in Array Using Nested Loops
5.1.
Example
5.2.
Algorithm
5.3.
Program
5.4.
Java
5.5.
Output
5.6.
Time Complexity
6.
Remove Duplicates Element in Array Using Hashmap Method
6.1.
Algorithm
6.2.
Program
6.3.
Java
6.4.
Output
6.5.
Time and Space Complexity
7.
Real-World Applications of Duplicate Detection in Arrays
8.
Frequently Asked Questions
8.1.
Can array list have duplicates?
8.2.
What are duplicate elements in Java?
8.3.
How do you find duplicates in an array of Java?
8.4.
How to remove duplicates in array using Java?
9.
Conclusion
Last Updated: Jul 4, 2025
Easy

Print Duplicate Elements in Array

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Dealing with arrays is a common task in programming, and one of the challenges developers often encounter is finding duplicate elements within an array. Whether you're working on data analysis, algorithmic problems, or software development, identifying and handling duplicate elements efficiently is crucial for maintaining the integrity and accuracy of your programs.

Print Duplicate Elements in Array

In this post, we will explore identifying and printing duplicate elements in an array using two methods. The first one is using two nested for loops and the second one is using the hashmap method. Before moving on to the program for displaying duplicate elements in an array, we will cover a few basics of the array data structure. 

I hope you will enjoy learning about this topic. So, let’s get started with our article on “Print Duplicate Elements in Array”.

Click here to know about Array in Javascript here.

What is duplicate elements in array?

Duplicate elements in an array are those elements that appear more than once within the array. In other words, if there are two or more occurrences of the same value in an array, those values are considered duplicates. Identifying duplicate elements in an array is a common programming task, and it often involves searching through the array to find and handle these repeated values appropriately.

Example of array declaration in Java:

int a[]=new int[10];

Why Identifying Duplicates Matters in Programming

Detecting duplicates in arrays is a critical task in many real-world programming scenarios. Whether you're working with user data, product listings, or search indexes, duplicates can lead to redundancy, increased memory usage, and inaccurate results. Ensuring unique data improves system performance, data integrity, and overall application reliability.

1. Data Validation
In applications like user registration, identifying duplicates prevents the same email or username from being used more than once. This ensures account uniqueness and helps maintain a secure, consistent user database.

2. Inventory Systems
In stock management or retail software, duplicate entries can inflate item counts or cause incorrect tracking. Detecting repeated products ensures accurate inventory records and smoother operations.

3. Search Optimization
Search engines or recommendation systems benefit from eliminating duplicates to speed up query results and provide cleaner, more relevant data to users, enhancing both performance and user experience.

By understanding how and where duplicates impact systems, developers can write more efficient and error-free code.

How to find Duplicates Elements in Array?

To find duplicate elements in a Java array, you can use various approaches. One common method is to use nested loops to compare each element with others. Here's a step-by-step explanation:

  • Initialize a loop that iterates over each element in the array.
     
  • For each element in the outer loop, start an inner loop that iterates from the next element to the end of the array.
     
  • Inside the inner loop, compare the current element from the outer loop with the elements in the inner loop.
     
  • If a match is found (i.e., if two elements have the same value), it means you've found a duplicate element.
     
  • You can then record or print the duplicate element, store it in a separate data structure, or take any other desired action.

 

Code

  • Java

Java

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

for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] == array[j]) {
System.out.println("Duplicate found: " + array[i]);
}
}
}
}
}
You can also try this code with Online Java Compiler
Run Code

Output

Duplicate found: 2
Duplicate found: 3


Check out this problem - Find Duplicate In Array

Remove Duplicates Element in Array Using Nested Loops

We need to print the duplicate elements in the array in this program. We can use two loops to do this. The first loop selects an element, while the second loop iterates across the array by comparing the selected element to other elements. If a duplicate element is detected, print it.

Example

Elements: 9 7 8 6 8 5 5 6 3 2

 

The first duplication in the above array occurs at index 4, duplicating the element (8) at index 2. In the above array, the duplicate entries are 5, 6, 8.

Algorithm

  1. Create and initialise an array.
  2. Use two loops to find duplicate components - the outer loop iterates across the array from 0 to the array's length. The outer loop will choose the element. The selected element will be compared to the rest of the array's elements using the inner loop.
  3. If there is a match, it signifies the duplicate element is there, so display it.

Program

  • Java

Java

/*Program to display duplicate elements of an array*/
import java.util.*;
class ArrayDuplicate{ 
   public static void main(String[] args) {     
       
       //Input the number of elements in the array. 
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter the size of array");
       int n=sc.nextInt();
       int arr[]=new int[n];
       //Input the elements of the array.
       System.out.println("Enter the elements of array");
       for(int i=0;i<n;i++)
       {
           arr[i]=sc.nextInt();
       }  //end of for loop 
       System.out.println("Duplicate elements are: "); 
       //Search for duplicate element 
       for(int i = 0; i < arr.length; i++) { 
           for(int j = i + 1; j < arr.length; j++) { 
               if(arr[i] == arr[j]) 
                   System.out.println(arr[j]); 
           } 
       } 
   } 
}
You can also try this code with Online Java Compiler
Run Code

Output

Enter the size of array
9
Enter the elements of array
9 7 8 6 8 5 5 6 3 2 
Duplicate elements are: 
8
6
5


We took the user’s inputs in the Java program above. We've now iterated all possible pairs in the array using two for loops. If two elements are equal, we display the element in the output.

Time Complexity

This method has a time complexity of O(n2). This is because we are iterating ‘n’ elements using two for loops.

Must Read Array of Objects in Java

Remove Duplicates Element in Array Using Hashmap Method

In this method, we have used the Hashmap in Java to find duplicate elements. The elements of the input array are stored as keys in the HashMap, and their occurrences are stored as values in the HashMap. Before going into the program, we will go through the algorithm to have a clear understanding of the underlying working steps of the program.

Algorithm

  1. Make a HashMap with a key and value pair of integers.
  2. Iterate through your array, checking whether each member is present in the HashMap using the ContainsKey() function.
  3. If it isn't already in the HashMap, use the put() function to add it.
  4. If it exists in the HashMap, mark it as a duplicate element and proceed.

Program

  • Java

Java

//java program to find duplicate elements in an array using hashmap
import java.util.*;
class ArrayDuplicate
{
   public static void main(String args[])
   {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter the size of array");
       int n=sc.nextInt();
       int a[]=new int[n];
       System.out.println("Enter the elements of array");
       for(int i=0;i<n;i++)
       {
           a[i]=sc.nextInt();
       }
       HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
       for(int i=0;i<n;i++)
       {
           if(hm.containsKey(a[i]))
           {
               hm.put(a[i],hm.get(a[i])+1);
           }
           else
           {
               hm.put(a[i],1);
           }
       }
       System.out.println("Duplicate elements are");
       for(Map.Entry<Integer, Integer> entry : hm.entrySet()) {
if(entry.getValue()>1) {
System.out.println(entry.getKey());
}
}
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

Enter the size of array
9
Enter the elements of array
9 7 8 6 8 5 5 6 3 2 
Duplicate elements are: 
8
6
5

 

Try it by yourself on java online compiler.

Time and Space Complexity

The time complexity is O(n). In the hashmap method, we are visiting each element of the array only once. So, the time complexity is of order ‘n.’

The space complexity is also O(n) to store the elements.

Also check out Addition of Two Numbers in Java.

Real-World Applications of Duplicate Detection in Arrays

Detecting duplicates in arrays goes far beyond basic coding exercises—it plays a vital role in building robust, real-world applications. Here are three practical scenarios where identifying duplicate elements is essential.

1. Duplicate Email Detection
In user registration systems, checking for duplicate emails ensures each account is tied to a unique identity. This prevents multiple registrations using the same email, which could lead to login conflicts or misuse.
Example: Before creating a new user account, the system checks if the entered email already exists in the database.

2. Removing Redundant Form Entries
Users may accidentally submit a form multiple times due to slow internet or clicking the submit button repeatedly. Detecting and removing these duplicate entries keeps the data clean and prevents unnecessary processing.
Example: A feedback form submission is checked against previous entries to avoid storing the same comment multiple times.

3. Detecting Fraud or Repeated Transactions
Financial and e-commerce platforms use duplicate detection to flag repeated transactions that could be accidental or fraudulent.
Example: A system compares recent payment requests to identify and block duplicate orders within a short timeframe, avoiding double charges.

Frequently Asked Questions

Can array list have duplicates?

Yes, an array can have duplicate values. 

What are duplicate elements in Java?

In Java, duplicate elements refer to having the same value or content appearing more than once in a data structure like an array, list, or set. It's when there are multiple identical elements in a collection.

How do you find duplicates in an array of Java?

To find duplicates in a Java array, you can use a HashSet or nested loops. With a HashSet, you add elements to the set as you iterate through the array, and if an element is already in the set, it's a duplicate. Using nested loops, compare each element with others and flag duplicates when found.

How to remove duplicates in array using Java?

To remove duplicates from a Java array, you can use a HashSet or create a new array. With a HashSet, add elements to the set while iterating through the original array, then convert the set back to an array. Alternatively, use nested loops to filter out duplicates and create a new array with unique elements.

Conclusion

In this article, we have discussed how to find duplicate elements in array. Understanding the problem of finding duplicate elements in arrays is a valuable skill for any programmer. Throughout this guide, we've explored various techniques and strategies, ranging from simple brute-force methods to more optimized algorithms.

Recommended Read: 

Live masterclass