Remove duplicate element in an array in java

Let’s learn to remove duplicate element in an array in java.

Remove duplicate element in an array in java

There are two ways to remove duplicate element from an array: first using temporary array and second using seperate index.

To remove duplicate element from an array, the array should be in sorted order.

remove duplicate element in an array in java

If array is not sorted then sort given array using Arrays.sort() method.

Now let’s see java program to remove duplicate element in an array using temporary array.

public class RemoveDuplicateElementDemo 
{
   public static int removeDuplicate(int[] arrNumbers, int num)
   {  
      if(num == 0 || num == 1)
      {  
         return num;  
      }  
      int[] arrTemporary = new int[num];  
      int b = 0;  
      for(int a = 0; a < num - 1; a++)
      {  
         if(arrNumbers[a] != arrNumbers[a + 1])
         {  
            arrTemporary[b++] = arrNumbers[a];  
         }  
      }  
      arrTemporary[b++] = arrNumbers[num - 1]; 
      for(int a = 0; a < b; a++)
      {  
         arrNumbers[a] = arrTemporary[a];  
      }  
      return b;  
   }
   public static void main(String[] args) 
   {
      int[] arrInput = {1, 2, 3, 3, 4, 5, 5, 6, 7, 8};  
      int len = arrInput.length;  
      len = removeDuplicate(arrInput, len);  
      // printing elements
      for(int a = 0; a < len; a++)
      {
         System.out.print(arrInput[a] + " ");
      }
   }
}


Output:

1 2 3 4 5 6 7 8


Also read – java program to remove vowels from a string using switch case


Now let’s learn to remove duplicate element using seperate index.

public class RemoveDuplicateUsingSeperateIndex
{
   public static int removeDuplicate(int[] arrNumbers, int num)
   {
      if(num == 0 || num == 1)
      {
         return num;
      }
      int b = 0;
      for(int a = 0; a < num - 1; a++)
      {
         if(arrNumbers[a] != arrNumbers[a + 1])
         {
            arrNumbers[b++] = arrNumbers[a];
         }
      }
      arrNumbers[b++] = arrNumbers[num - 1];
      return b;
   }
   public static void main(String[] args)
   {
      int[] arrNumbers = {1, 2, 3, 3, 4, 5, 5, 6, 7, 8};
      int len = arrNumbers.length;
      len = removeDuplicate(arrNumbers, len);
      for(int a = 0; a < len; a++)
      {
         System.out.print(arrNumbers[a] + " ");
      }
   }
}


Output:

1 2 3 4 5 6 7 8


Meanwhile we can also remove duplicates from unsorted array – java. To do that first we need to sort given array.

To sort array we are going to use Arrays.sort() method. Now let’s see program.

import java.util.Arrays;
public class RemoveDuplicateExample 
{
   public static int removeDuplicate(int[] arrNumbers, int num)
   {  
      if(num == 0 || num == 1)
      {  
         return num;  
      }  
      int[] temp = new int[num];  
      int b = 0;  
      for(int a = 0; a < num - 1; a++)
      {  
         if(arrNumbers[a] != arrNumbers[a + 1])
         {  
            temp[b++] = arrNumbers[a];  
         }  
      }  
      temp[b++] = arrNumbers[num - 1];
      for(int a = 0; a < b; a++)
      {  
         arrNumbers[a] = temp[a];  
      }  
      return b;  
   }
   public static void main(String[] args) 
   {
      int[] arrInput = {2, 12, 10, 6, 8, 8, 2, 14, 12 ,4};
      // first sort array
      Arrays.sort(arrInput);  
      int len = arrInput.length;  
      len = removeDuplicate(arrInput, len);
      for(int a = 0; a < len; a++)
      {
         System.out.print(arrInput[a] + " "); 
      }
   }
}


Output:

2 4 6 8 10 12 14


How to remove duplicates from array in java without using collections

Let’s learn how to remove duplicates from array in java without using collections.

import java.util.Arrays;
public class WithoutUsingCollections
{
   static void removeDuplicateWithoutCollections(int[] arrDuplicate)
   {
      System.out.println("Before removing duplicates from array: ");
      for(int a = 0; a < arrDuplicate.length; a++)
      {
         System.out.print(arrDuplicate[a] + " ");
      }
      int sizeUnique = arrDuplicate.length;
      // compare each element with other element
      for(int a = 0; a < sizeUnique; a++)
      {
         for(int b = a + 1; b < sizeUnique; b++)
         {
            // if any two numbers are equal
            if(arrDuplicate[a] == arrDuplicate[b])
            {
               arrDuplicate[b] = arrDuplicate[sizeUnique - 1];
               sizeUnique--;
               b--;
            }
         }
      }
      int[] arrWithoutDuplicate = Arrays.copyOf(arrDuplicate, sizeUnique);
      // print elements array without duplicate elements
      System.out.println();
      System.out.println("After removing duplicates from array: ");
      for(int a = 0; a < arrWithoutDuplicate.length; a++)
      {
         System.out.print(arrWithoutDuplicate[a] + " ");
      }
      System.out.println();
   }
   public static void main(String[] args)
   {
      removeDuplicateWithoutCollections(new int[] {1, 3, 5, 1, 7, 9});
      removeDuplicateWithoutCollections(new int[] {56, 85, 56, 85, 38, 28});
   }
}


Output:

Before removing duplicates from array:
1 3 5 1 7 9
After removing duplicates from array:
1 3 5 9 7

Before removing duplicates from array:
56 85 56 85 38 28
After removing duplicates from array:
56 85 28 38


How to remove duplicates from arraylist in java

Here’s the java program to remove duplicates from arraylist.

import java.util.ArrayList;
import java.util.Arrays;
public class DuplicatesFromArraylist
{
   public static <T> ArrayList<T> removeDuplicate(ArrayList<T> al)
   {
      ArrayList<T> alNew = new ArrayList<T>();
      for(T element : al)
      {
         // if element is present in alNew then add it
         if(!alNew.contains(element))
         {
            alNew.add(element);
         }
      }
      // return alNew
      return alNew;
   }
   public static void main(String[] args)
   {
      ArrayList<Integer> al = new ArrayList<>(Arrays.asList(2, 2, 4, 4, 6, 6, 8, 8, 1, 3, 5, 7, 9));
      System.out.println("Before removing duplicates from ArrayList: " + al);
      ArrayList<Integer> alNew = removeDuplicate(al);
      // printing ArrayList with duplicates
      System.out.println("After removing duplicates from ArrayList: " + alNew);
   }
}


Output:

Before removing duplicates from ArrayList: [2, 2, 4, 4, 6, 6, 8, 8, 1, 3, 5, 7, 9]
After removing duplicates from ArrayList: [2, 4, 6, 8, 1, 3, 5, 7, 9]


remove duplicates from string array java

Now let’s to remove duplicates from string array java.

import java.util.Arrays;
import java.util.LinkedHashSet;
public class DuplicatesFromString
{
   public static void main(String[] args)
   {
      String[] strAnimals = {
              "lion",
              "bear",
              "deer",
              "lion",
              "elephant",
              "deer"
      };
      System.out.println("Before removing duplicates from string array: " + Arrays.toString(strAnimals));
      // convert given string array to list add elements to LinkedHashSet
      // LinkedHashSet will remove duplicate elements
      LinkedHashSet<String> lhsAnimals = new LinkedHashSet<String>(Arrays.asList(strAnimals));
      String[] strNewAnimals = lhsAnimals.toArray(new String[lhsAnimals.size()]);
      System.out.println("After removing duplicates from string array: " + Arrays.toString(strNewAnimals));
   }
}


Output:

Before removing duplicates from string array: [lion, bear, deer, lion, elephant, deer]
After removing duplicates from string array: [lion, bear, deer, elephant]


remove duplicates from array java 8

To remove duplicates from array in java 8 use distinct() method.

distinct() method returns a stream consisting of the distinct elements (according to Object.equals(Object)) of given stream.

Let’s see an example.

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class RemoveDuplicatesJava8
{
   public static void main(String[] args)
   {
      List li1 = new ArrayList<>(Arrays.asList(2, 2, 4, 4, 6, 6, 8, 8, 1, 3, 5, 7, 9));
      System.out.println("Before removing duplicate elements: " + li1);
      // create new list from elements of original list
      List li2 = li1.stream().distinct().collect(Collectors.toList());
      // printing ArrayList with duplicates removed
      System.out.println("After removing duplicate elements: " + li2);
   }
}


Output:

ArrayList with duplicates: [2, 2, 4, 4, 6, 6, 8, 8, 1, 3, 5, 7, 9]
ArrayList with duplicates removed: [2, 4, 6, 8, 1, 3, 5, 7, 9]