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.

How to remove duplicates from array in java without using collections

To remove duplicates from array is bit tricky. Because problem with arrays is not finding duplicates, its about removing duplicates.

We all know that an array is a static data structure and its length is fixed. If you are deleting an element from an array you need to create a new array and copy that content into that new array.

If an array contain lot of duplicate elements this could result in lots of temporary arrays. So here in this post we going to see a logic to remove duplicates from array without using collections.

In the below java program we are creating a static method removeDuplicateWithoutCollections() which is called from main method.

In this method first we are going to print array on console with duplicate elements. Then find the size of the array with duplicate elements.

Here’s the logic to remove duplicates from array. Now using two for loops compare each element with other element and then using if statement check if any two numbers are equal.

Finally create new array “arrWithoutDuplicate” and store elements without duplicates and print on the console using for loop. Here’s an example 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


Also read – java overview