Register Login

Remove Duplicates from Array in JAVA

Updated Oct 09, 2019

In this tutorial you will learn how to remove duplicates from array in java using following method:

  • Remove duplicates from Sorted Array
  • Remove duplicates from Unsorted Array
  • Using Java LinkedHashSet class

Remove duplicates from Sorted Array

Example:

//Java program to remove duplicate elements from array
//From Sorted Array

//Main Class of the program
public class Main{
//Custom Function
public static int removeDuplicateElementsFromArray(int array[], int arrayLength){
        //Checking the length of the passed array
        if (arrayLength==0 || arrayLength==1){
            return arrayLength;
        }
        //Creating a temporary array of the passed array lenght
        int[] temp = new int[arrayLength];
        int j = 0;
        //For Loop itteration
        for (int i=0; i<arrayLength-1; i++){
            if (array[i] != array[i+1]){
                temp[j++] = array[i];
            }
         }
        //Setting the last element of array to the temp
        temp[j++] = array[arrayLength-1];
        // Changing original array
        for (int i=0; i<j; i++){
            array[i] = temp[i];
        }
        return j;
    }

    //Main method of the program
    public static void main (String[] args) {
        //Declaring and Initilizing of arrray
        int arr[] = {10,10,20,20,20,30,30,40,40,40,50,50,50};
        //Get the lenght of the array
        int n = arr.length;
        //Calling the custom function to remove duplicate elements
        n = removeDuplicateElementsFromArray(arr, n);
        //printing array elements
        for (int i=0; i<n; i++)
           System.out.print(arr[i]+" ");
    }
}

Output:

10 20 30 40 50 

Remove duplicates from Unsorted Array

Example:

//Java program to remove duplicate elements from array
//From Unsorted Array

//Importing the Array from the util package of java
import java.util.Arrays;

//Main Class of the program
public class Main{
//Custom Function
public static int removeDuplicateElementsFromArray(int array[], int arrayLength){
        //Checking the length of the passed array
        if (arrayLength==0 || arrayLength==1){
            return arrayLength;
        }
        //Creating a temporary array of the passed array lenght
        int[] temp = new int[arrayLength];
        int j = 0;
        //For Loop itteration
        for (int i=0; i<arrayLength-1; i++){
            if (array[i] != array[i+1]){
                temp[j++] = array[i];
            }
         }
        //Setting the last element of array to the temp
        temp[j++] = array[arrayLength-1];
        // Changing original array
        for (int i=0; i<j; i++){
            array[i] = temp[i];
        }
        return j;
    }

    //Main method of the program
    public static void main (String[] args) {
        //Declaring and Initilizing of arrray
        int arr[] = {10,20,8,57,23,23,8,10,57,20};
        //sort function of array to sort the elements
        Arrays.sort(arr);
        //Get the lenght of the array
        int n = arr.length;
        //Calling the custom function to remove duplicate elements
        n = removeDuplicateElementsFromArray(arr, n);
        //printing array elements
        for (int i=0; i<n; i++)
           System.out.print(arr[i]+" ");
    }
} 

Output:

8 10 20 23 57 

Using Java LinkedHashSet class

Example:

//Java code to remove duplicate from array

//Using Java LinkedHashSet class

//Importing the Util Package
import java.util.*;

//Main Class of program
public class Main
{
    //Main method of the program
    public static void main(String[] args) throws CloneNotSupportedException{
        //Variable to take number of elements in Array
        int n;
        //Creating object for Scanner Class
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the Number of elements in array : ");
        //Taking input from user
        n = input.nextInt();
        //Creating the Integer type array variable of `n` size
        Integer[] arr = new Integer[n];
        //For loop for iteration
        for(int i = 0; i < n; i++){
            //Taking input on every iteration for `i` th location
            arr[i] = input.nextInt();
        }
        //Printing Duplicate array
        System.out.println("With Duplicate Values : "+Arrays.toString(arr));
        //Creating Set from array
        LinkedHashSet<Integer> hashTable = new LinkedHashSet<>( Arrays.asList(arr) );
        //printing the output
        System.out.println("Without Duplicate Values : "+hashTable.toString());
    }
}

Output:

Enter the Number of elements in array : 3
1
22
22
With Duplicate Values : [1, 22, 22]
Without Duplicate Values : [1, 22] 

 


×