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

To remove duplicate element from an array first the array must be in sorted order or remove duplicates from sorted array – java.

Here in this post we are going to learn to remove duplicate element in array using temporary array.

remove duplicate element in an array in java

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

public class RemoveDuplicatesDemo 
{
   public static int duplicateElement(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 = {1, 2, 3, 3, 4, 5, 5, 6, 7, 8};  
      int len = arrInput.length;  
      len = duplicateElement(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

In the above program we saw that to remove duplicate elements the condition was array should be in sorted order.


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


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


Remove duplicates from arraylist java

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
public class DuplicatesFromArraylist 
{
   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 java: " + al);
      LinkedHashSet<Integer> lhs = new LinkedHashSet<>(al);
      ArrayList<Integer> alDuplicates = new ArrayList<>(lhs);
      System.out.println("After removing duplicates from arraylist java: " + alDuplicates);
   }
}


Output:

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


Java program to remove duplicates from string array

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

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 lhsAnimals = new LinkedHashSet(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. Let’s see an example.

import java.util.Arrays;
public class RemoveDuplicatesJava8
{
   public static void main(String[] args)
   {
      String[] strFruits = {
              "apple",
              "banana",
              "orange",
              "apple",
              "grapes",
              "banana"
      };
      strFruits = Arrays.stream(strFruits).distinct().toArray(String[]::new);
      System.out.println("Array after removing duplicates from array: " + Arrays.toString(strFruits));
   }
}


Output:

Array after removing duplicates: [apple, banana, orange, grapes]