How to remove duplicate values from integer array in java / Various ways to remove duplicate elements from integer array in java

Removing duplicate integers from an array is a task that most of us have faced at work. It is also a commonly asked question in technical interview process. There are many ways in java to do this. All are listed below.
Before diving into the methods, let’s define the array which we will be using in all the methods. The array will contain following elements :

int[] arrayWithDuplicates = { 1, 2, 34, 10, 2, 1, 5, 34 };

Method 1: Comparing individual elements

This method involves iterating over the array, comparing each element with other elements for equality and taking appropriate action to remove one of the duplicate elements. Let’s see the code and then get into its detail :

static void eleminateDuplicates(int[] arrayWithDuplicates){
        //Assuming all elements in input array are unique
	int noOfUniqueElements = arrayWithDuplicates.length;
        // get the size of the array
        int size = arrayWithDuplicates.length;
	//Comparing each element with all other elements
	for (int i = 0; i < size; i++) {
            // iterate over elements ahead of the current loop element
	    for (int j = i+1; j < size; j++) {
                //check for equality
		if(arrayWithDuplicates[i] == arrayWithDuplicates[j]) {
                     //Replace duplicate element with last unique element
		     arrayWithDuplicates[j] = arrayWithDuplicates[size-1];
		     /* Decrementing size as the element at last location 
                      * has already been copied at some start location  */
		     size--;
		     /*decrement inner loop counter since the new element 
                     * also needs to be compared */ 
		     j--;
                     // reduce the number of unique elements since we have found a duplicate element
                     noOfUniqueElements--;  
		}
            }
	}
	//Copying only unique elements of arrayWithDuplicates into arrayWithoutDuplicates
	int[] arrayWithoutDuplicates = Arrays.copyOf(arrayWithDuplicates, noOfUniqueElements);
	//Printing arrayWithoutDuplicates
	System.out.println("Array Without Duplicates : ");
	for (int i = 0; i < arrayWithoutDuplicates.length; i++){
             System.out.print(arrayWithoutDuplicates[i]);
             System.out.print(" ");
	}
}

Output will be :

Array Without Duplicates :
1 2 34 10 5

Details :

This method sets up two loops : Outer loop for iterating entire array and inner loop starting at one index greater than the outer loop index. So at every iteration of the outer loop, an element at the current location of the array is compared with all the elements ahead it in the array. Now as soon as two elements match, means a duplicity is detected following actions are performed. Note that at this point i=0, j=5,size=8 :

  1. The element at the position of the value of sizevariable (which will be equal to the length of the array when first duplicate is detected) is swapped with the element at the current index of the inner loop. Thus, if we consider our sample array { 1, 2, 34, 10, 2, 1, 5, 34 }, then after this step it will become {1, 2, 34, 10, 2, 34, 5, 34} since element at position 6 (j=5), which is 1, matches the first element (i=0) and gets replaced by 34 whose position is at index size-1.
  2. Since the element (34) at last position is already included in the array, the last position should be decreased. Hence, size--.
  3. Now the element at position j is new and has not been compared with the other elements so j is reduced by one.

Note : Whenever a duplicate element is detected, the value of sizeis decreased by 1 which means first the last element will be replaced with duplicate element, then second last and so on.

After both the iterations complete, all the duplicate array elements would be placed at the end of the array with the size variable containing the number of unique elements in the array. Finally, this array is copied to a new array with its length equal  to the number of unique elements in the original array (that is, value of size) using copyOfmethod of java.util.Arraysclass.

Listed below are the contents of the array at every point when a duplicate element is detected during iteration along with the values of i, j and size variables :

arrayijsize
{1, 2, 34, 10, 2, 34, 5, 34}058
{1, 2, 34, 10, 5, 34, 5, 34}147
{1, 2, 34, 10, 5, 34, 5, 34}256

Method 2 : Using Set Collection

This method utilizes fundamental property of a set that it only holds unique elements and discards duplicate elements. Initialize a java.util.Set. Iterate over the array and in each iteration add its elements to the set. The set will only add unique elements thus removing all duplicate elements. Convert this set to an array using its toArraymethod.

static void eliminateDuplicateUsingSet(int[] arrayWithDuplicates) {
	Set set = new HashSet();
	for (int counter = 0; counter < arrayWithDuplicates.length; counter++) {
		set.add(arrayWithDuplicates[counter]);
	}
	Integer[] newArr = set.toArray(new Integer[0]);
	System.out.println("Array without duplicates");
	// print the unique array
	for (int counter = 0; counter < newArr.length; counter++) {
		System.out.print(newArr[counter]);
                System.out.print(" "); 
	}
}

Output will be :

Array Without Duplicates :
1 2 34 10 5

Method 3 : Using List Collection

Initialize a java.util.ArrayListobject. Iterate over the array and in each iteration check it the current array element exists in the list. If it does not, then add the element to it. After iteration completes, the array list contains only unique elements of the array. Convert it to a integer array using toArraymethod of java.util.ArrayList.

static void eliminateDuplicateUsingArrayList(int[] arrayWithDuplicates) {
     ArrayList list = new ArrayList();
     for (int counter = 0; counter < arrayWithDuplicates.length; counter++) {
	if (!list.contains(arr[counter]))
            list.add(arrayWithDuplicates[counter]);
	}
     System.out.println("Array without duplicates");
     Integer[] arrayWithoutDuplicates = list.toArray(new Integer[0]);
     // print the unique array
     for (int counter = 0; counter < arrayWithoutDuplicates.length; counter++) {
	System.out.println(arrayWithoutDuplicates[counter]);
        System.out.print(" ");
     }
}

Output :

Array Without Duplicates :
1 2 34 10 5

Method 4: Using streams in Java 8

This is the smallest and easiest method to remove duplicates from an array which uses java.util.streamapi introduced in java 8.Get a java.util.Streamobject by calling stream()method of java.util.Arraysclass.Calling distinct()method on this stream again returns the stream object. distinct()method returns a stream of unique elements. Again calling toArray()method on this stream object returns the array representation of this stream.

static void eliminateUsingJava8(int[] arrayWithDuplicates) {
	int[] arrayWithOutDuplicates= Arrays.stream(arrayWithDuplicates).distinct().toArray();
	System.out.println("Array without duplicates");
	// print the unique array
	for (int counter = 0; counter < arrayWithOutDuplicates.length; counter++) {
	    System.out.println(arrayWithOutDuplicates[counter]);
            System.out.print(" ");   
	}
}

Output :

Array Without Duplicates :
1 2 34 10 5

Don't hesitate to add any other methods to the list or for any suggestions for optimizing the above methods. coDippa !!!

4 Comments

  1. int []arr={8,4,2,5,6,3,1,8,5,6,2,1};
    how to do it 6 is not getting elliminated

    1. Author

      There was a line of code missing in Method 1 due to which it was not removing element.
      Please check now and thank you for pointing out.
      Keep visiting!!

Leave a Reply