Find duplicate elements in an array

This is most common interview question in java now-a-days. There are many techniques to find duplicate elements in array in java like using Collections.frequency(). I am writing yet another solution which is much easier and fast.

Here an array of integers is having 10 integers and 1 and 8 are duplicate integers. You need to filter them out.

package com.howtodoinjava.interview;

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

public class DuplicateInArray 
{
	public static void main(String[] args) 
	{
		int[] array = {1,1,2,3,4,5,6,7,8,8};
		
		Set<Integer> set = new HashSet<Integer>();
		
		for(int i = 0; i < array.length ; i++) 
		{
			//If same integer is already present then add method will return FALSE 
			if(set.add(array[i]) == false) 
			{
				System.out.println("Duplicate element found : " + array[i]);
			}
		}
	}
}

Output:

Duplicate element found : 1
Duplicate element found : 8

In this solution, all you have to do is to iterate over array and all elements in a set one by one. If Set.add() method returns false then element is already present in set and thus it is duplicate.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

23 thoughts on “Find duplicate elements in an array”

  1. In interview i have faced one question.
    How to find second repeated value in Array?
    Ex: {1,2,3,4,7,8,4,6,9,5,8,0,1}
    o/p: 8

    What is the solution?

    Reply
    • public static void main(String []args){
      int[] array={2,3,5,2,5,6,9,6,4,7};
      int count=0;
      int newcount;
      int[] duparray=new int[count];
      for(int i=0;i<array.length;i++){
      for(int j=i+1;j<array.length;j++){
      if(array[i]==array[j]){
      count++;
      if(count==2){
      System.out.println("The Second repeated value in an array is" +array[i]);
      }
      }

      }

      }

      }

      Reply
  2. Hi,

    Is this optimized one ? time complexity O(n)

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int A[] = {4,2,6,5,3,8,6};
    List ar = Arrays.asList(IntStream.of(A).boxed().toArray());
    int dist[] = Arrays.stream(A).distinct().toArray();
    int count;
    int maxCount =0;
    int leader = 0;

    for(int k : dist){
    count = Collections.frequency(ar, k);
    if(count > maxCount ){
    leader = k;
    maxCount = count;
    }
    }
    System.out.println(“Leader of array:”+leader);

    }

    Reply
  3. To avoid printing many times, another solution for taking the strategy space for time:

    int[] array={10,10,20,10,20,41,55,10,10,10,11,12,66};
    Set set = new HashSet();
    Set set2 = new HashSet();
    for(int i=0; i<array.length; i++){
    if(!set.add(array[i])){
    if(set2.add(array[i]))
    System.out.println(array[i]);
    }
    }

    Reply
  4. Hello,
    int[] arr={10,10,20,10,20,41,55,10,10,10,11,12,66};
    Your program may fail for this array declaration it will print 5 times 10 s
    so make it correct as below:

    int[] arr={10,10,20,10,20,41,55,10,10,10,11,12,66};
    Set set=new HashSet();

    for(int i=0;i<arr.length;i++)
    {
    for(int j=i+1;j<arr.length;j++)
    {
    if(arr[i]==arr[j])
    {
    if(!set.contains(arr[i]))
    {
    set.add(arr[i]);
    System.out.println("Duplicate Element:"+arr[i]);
    }
    }
    }//end inner for;
    }//end outer for

    Reply
  5.  
    public class dupliacteselection {
    
    	public static void main(String[] args) {
    	    int a[]={1,2,3,3,4};
    
    	    Set<Integer> s=new HashSet<Integer>();
    	    for(int i=0;i<a.length;i++)
    	    {
    	    if(!s.add(a[i]))
    	        System.out.println("at index"+ i+" "+a[i]+"is duplicate");  
    	    }
    	    for(int i:s)
    	    {
    	        System.out.println(i);
    	    }
    	}
    
    }
     
    Reply
  6. @Lokesh perfect one just to add another similiar approach for this is ..

    public class dupliacteselection {

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

    Set s=new HashSet();
    for(int i=0;i<a.length;i++)
    {
    if(!s.add(a[i]))
    System.out.println("at index"+ i+" "+a[i]+"is duplicate");
    }
    for(int i:s)
    {
    System.out.println(i);
    }
    }

    }

    Reply
  7. import java.util.HashSet;
    import java.util.Set;

    public class DuplicatItems {

    public static void main(String[] args) {
    int items[] = {1,2,3,2,5,7,6,4,5,5,9};
    Set set = new HashSet();
    for (int item:items){
    if(set.contains(item))
    System.out.println(item +” is duplicate item”);
    set.add(item);
    }
    System.out.println(“No. of duplicate items: “+(items.length-set.size()));
    }
    }

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.