Remove duplicates from unsorted array? – java – Java Program to Remove Duplicate Elements in an Array

Remove duplicates from unsorted array? – java: In the previous article, we have seen Java Program to Find the Third Smallest Number in an Array

In this article we are going to remove duplicates elements in an array in Java

Java Program to Remove Duplicate Elements in an Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to remove duplicate values in an array.

Note: To remove duplicate element from an array of integers we will sort the array first, if it is not sorted, then we can implement our logic.

Method-1: Java Program to Remove Duplicate Elements in an Array By Using an Extra Array

Approach:

  • Create an array with sorted elements.
  • Display the array.
  • Create another blank array of same size as the original array.
  • Traverse through the array and copy all non duplicate elements from the array by comparing them to the next element. Use a counter to count the original elements.
  • Copy the elements back to the main array.
  • Display the main array.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        // Crating an array
        int arr[] = {-7, 1, 5, 2,2,2 -4, 3, 0};
        // Displaying the array
        System.out.print("Before removing duplicate elements, array: ");
        printArray(arr);
        // Print non duplicate elements
        removeDupes(arr,arr.length);
    }

    // Function to print the array
    static void printArray(int arr[])
    {
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
    
    public static void removeDupes(int arr[], int size)
    {  
        // Creating a second array to hold temporary elements
        int[] temp = new int[size];  
        int j = 0;  
        // Checks for duplicate elements
        for (int i=0; i<size-1; i++)
        {  
            if (arr[i] != arr[i+1])
            {  
                temp[j++] = arr[i];  
            }  
        }  
        temp[j++] = arr[size-1];     
        // Modifying the values in the original array 
        for (int i=0; i<j; i++)
        {  
            arr[i] = temp[i];  
        }  
        // Prints the modified array
        System.out.print("After removing duplicate elements, array : ");
        for(int i=0;i<j;i++)
            System.out.print(arr[i]+" ");
    }  
}

Output:

Before removing duplicate elements, array: -7 1 5 2 2 -2 3 0 
After removing duplicate elements, array : -7 1 5 2 -2 3 0

Method-2: Java Program to Remove Duplicate Elements in an Array Without Using an Extra Array

Approach:

This approach is similar to the first approach, however only difference is we are using the same array not an extra array and the elements are not in sorted order. So, we will sort the array first then we will implement our logic. It is more efficient than the previous approach.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        // Crating an array
        //unsorted array
        int arr[] = {5,8,7,9,6,5,4,3,7,1,6,2};
        // Displaying the array
        System.out.print("Before removing duplicate elements, array: ");
        printArray(arr);
        //Sort the array before removing the duplicates
        Arrays.sort(arr);
        // Print non duplicate elements
        removeDupes(arr,arr.length);
    }

    // Function to print the array
    static void printArray(int arr[])
    {
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
    
    public static void removeDupes(int arr[], int size){  
        // Counter
        int j = 0;  
        // Checks for duplicate elements
        for (int i=0; i<size-1; i++)
        {  
            if (arr[i] != arr[i+1])
            {  
                arr[j++] = arr[i];  
            }  
        }  
        arr[j++] = arr[size-1];     
        // Prints the modified array
        System.out.print("After removing duplicate elements, array: ");
        for(int i=0;i<j;i++)
            System.out.print(arr[i]+" ");
    }  
}

Before removing duplicate elements, array: 5 8 7 9 6 5 4 3 7 1 6 2 
After removing duplicate elements, array: 1 2 3 4 5 6 7 8 9

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: