Remove duplicate elements in an array in java

Let’s learn remove duplicate elements in an array in java.

Remove duplicate elements in an array in java

There are two ways to remove duplicates from array in java: first using temporary array and second using seperate index. To remove duplicates elements from array in java, the array should be in sorted order.

remove duplicate elements in an array in java

If array is not sorted then sort given array using Arrays.sort() method. In the below java program we are removing duplicate elements from array using temporary array. Now let’s see program to remove duplicate elements in an array.

public class RemoveDuplicateElementsDemo 
{
   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


Remove duplicate elements in an array using seperate index

Now let’s learn program to remove duplicate elements in an array 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


Remove duplicate elements from unsorted array

Meanwhile we can also remove duplicate elements from unsorted array. 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