Java – Remove duplicates from array

Learn to remove duplicate elements from an array in Java using different techniques such as LinkedHashSet from Collections framework and using a temporary array.

1. Remove duplicates from array using LinkedHashSet

Using Java Collections, LinkedHashSet is one of the best approaches for removing the duplicates from an array. LinkedHashSet achieves two objectives :

  • Removes the duplicate elements, as well as
  • Maintains the ordering of elements

Java program to remove duplicates in array using LinkedHashSet

In given example, numbers is an integer array which has duplicate numbers 1, 3 and 5. We will add all elements from the array into the LinkedHashSet, and then we will convert the linkedhashSet to the array.

The result array does not have duplicate integers.

import java.util.Arrays;
import java.util.LinkedHashSet;

public class RemoveDuplicateExample
{
    public static void main(String[] args) throws CloneNotSupportedException 
    {
        //Array with duplicate elements
        Integer[] numbers = new Integer[] {1,2,3,4,5,1,3,5};
        
        //This array has duplicate elements
        System.out.println( Arrays.toString(numbers) );
        
        //Create set from array elements
        LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>( Arrays.asList(numbers) );
        
        //Get back the array without duplicates
        Integer[] numbersWithoutDuplicates = linkedHashSet.toArray(new Integer[] {});
        
        //Verify the array content
        System.out.println( Arrays.toString(numbersWithoutDuplicates) );
    }
}

Program Output.

[1, 2, 3, 4, 5, 1, 3, 5]
[1, 2, 3, 4, 5]

2. Remove duplicate elements from array using temporary array

If we are not allowed to use collections API (e.g. in a Java interview), we can use one of these approaches according to the question.

2.1. Removing duplicates from a sorted array

If array elements are sorted already then removing duplicates involve following steps:

  • Create a new array 'tempArray' with same size as original array 'origArray'.
  • Iterate over array starting from index location ‘0’.
  • Match current element with next element indexes until mismatch is found.
  • Add element to 'tempArray' and make current element to eleemnt which was mismatched.
  • Continue the iteration.
import java.util.Arrays;

public class RemoveDuplicateExample
{
    public static void main(String[] args) throws CloneNotSupportedException 
    {
        // Array with duplicate elements
        Integer[] origArray = new Integer[] { 1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8 };

        // This array has duplicate elements
        System.out.println(Arrays.toString(origArray));

        Integer[] tempArray = removeDuplicates(origArray);

        // Verify the array content
        System.out.println(Arrays.toString(tempArray));
    }

    private static Integer[] removeDuplicates(Integer[] origArray) {

        Integer[] tempArray = new Integer[origArray.length];
        
        int indexJ = 0;
        for (int indexI = 0; indexI < origArray.length - 1; indexI++) 
        {
            Integer currentElement = origArray[indexI];
            
            if (currentElement != origArray[indexI+1]) {
                tempArray[indexJ++] = currentElement;
            }
        }
        
        tempArray[indexJ++] = origArray[origArray.length-1];
        
        return tempArray;
    }
}

Program Output.

[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, null, null, null, null, null]

2.1. Removing duplicates from an unsorted array

package com.howtodoinjava;

import java.util.Arrays;

public class RemoveDuplicatesExample {
	public static void main(String[] args) throws CloneNotSupportedException {
		// Array with duplicate elements
		Integer[] origArray = new Integer[] { 1, 2, 4, 9, 7, 3, 4, 5, 8, 6, 1, 7, 8 };

		// This array has duplicate elements
		System.out.println(Arrays.toString(origArray));

		Integer[] tempArray = removeDuplicates(origArray);

		// Verify the array content
		System.out.println(Arrays.toString(tempArray));
	}

	private static Integer[] removeDuplicates(Integer[] origArray) {

		for (int j = 0; j < origArray.length - 1; j++) {
			for (int i = j + 1; i < origArray.length - 1; i++) {
				if (origArray[j] == origArray[i]) {
					origArray[i] = null;
				}
			}
		}

		origArray[origArray.length - 1] = null;

		return origArray;
	}
}

Program Output.

[1, 2, 4, 9, 7, 3, 4, 5, 8, 6, 1, 7, 8]
[1, 2, 4, 9, 7, 3, null, 5, 8, 6, null, null, null]

Use the above examples if you are asked to remove duplicates from an array in Java without using collections api.

3. Removing duplicates using Streams

The Java 8 Stream.distinct() method returns an Stream consisting of the distinct elements of a given Stream.

Note that the element equality is checked according to element’s equals() method.

Integer[] origArray = new Integer[] { 1, 2, 4, 9, 7, 3, 4, 5, 8, 6, 1, 7, 8 };

List<Integer> listWithoutDuplicates = Arrays.asList(origArray)
                        .stream()
    			.distinct()
    			.collect(Collectors.toList());
 
System.out.println(listWithoutDuplicates);

Program output:

[1, 2, 3, 4, 5, 6, 7, 8]

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.

8 thoughts on “Java – Remove duplicates from array”

  1. 100% work in any case!!!

    import java.util.Arrays;
    
    public class Duplicateremove {
    public static void main(String [] args)
    {
        int [] a= { 1, 2,0,0, 5, 1, 7, 2, 4, 2};
        int c=0;
        int d=0;
    
        Arrays.sort(a);
    
        for (int i = 0; i < a.length; i++) {
    
        for (int  j = i+1;  j < a.length;  j++) {
            if (a[i]==a[j])
                
            {
            a[i]=0; 
            c++;
            }
            else 
                {
                    continue;
                }
            
        }   
        }
        
        for (int i = 0; i <a>c)
        {
            System.out.println("0");
        }
        
        for (int i = 0; i < a.length; i++) {
            if(a[i]!=0)
            {
                System.out.println(a[i]);
            }
        }
        
    }
    }
    Reply
  2. 
    public class Repeatremove {
    	public static void main(String[] args) {
    
    
    		int a[]={1,2,3,3,6,7,8,9,10};
    		int s=a.length;
    		for(int i=0;i<s-1;i++)	
    		{
    			for(int j=i+1;j<a.length;j++)
    			{
    				if(a[i]==a[j])
    				{
    
    					a[i]=0;
    
    					//a[i]=a[j];
    					//System.out.println(a[i]+""+a[j]);
    
    				}
    
    			}
    
    		}
    		for(int i=0;i<s;i++)	
    		{
    			System.out.println(a[i]);
    
    		}
    
    	}
    }
    
    Reply
  3. The code for Unsorted Array is not correct, something like below will work:

     
    private static Integer[] removeDuplicatesUnSorted(Integer[] origArray) {
    
        for (int j = 0; j < origArray.length - 1; j++) {
          for (int i = j+1; i < origArray.length - 1; i++) {
            if (origArray[j] == origArray[i]) {
              origArray[i] = null;
            }
          }
        }
    
        origArray[origArray.length-1] = null;
    
        return origArray;
      }
    
    Reply
    • In Kotlin way without contains use. Checking nulls in inner loop.

      fun noDuplicatesInArray(array: Array<Int?>) {
          var current: Pair<Int, Int?>
          for (index in array.indices) {
              array[index]?.run {
                  current = Pair(index, array[index])
                  for(innerIndex in current.first + 1 until array.size) {
                      array[innerIndex]?.run {
                          if (current.second == array[innerIndex]) {
                              array[innerIndex] = null
                          }
                      }
                  }
              }
          }
      }
      
      Reply

Leave a Comment

HowToDoInJava

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