Java Program to remove duplicate elements from array

Java Program to remove duplicate elements from array

There are multiple ways to delete all duplicate elements from an arrays. To delete the given element from array you can use third temporary array or by sorting the array and then removing the duplicate elements.

We have kept the different logics for sorted and unsorted array in this post.

For example:

Example 1:

Suppose we have a given sorted array arr=[10, 20, 30, 30, 30, 40, 50, 50]

then output will be [10, 20, 30, 40, 50]

Example 2:

Suppose we have a given unsorted array arr = [20, 30, 10, 20, 20, 30, 40, 10]

then output should be [10, 20, 30, 40]

Program 1 : Java Program to remove duplicates in Sorted Array

import java.util.*; 
public class Main
{
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	  
	    //taking array size and array element as input
        System.out.println("Enter the size of array: ");
        int n = sc.nextInt();
        int j=0;
        int arr[] = new int[n];
        int[] temp = new int[n];  
        System.out.println("Please give array input in sorted form ");
        for(int i=0; i<n; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr[i] = sc.nextInt();
        }
        System.out.print("Array after removing duplicate values: ");
      
        //complete logic to removing duplicate values in sorted array
        for (int i=0; i<n-1; i++){  
            if (arr[i] != arr[i+1]){  
                temp[j++] = arr[i];  
            }  
        }  
        temp[j++] = arr[n-1];     
        for (int i=0; i<j; i++){  
            arr[i] = temp[i];  
        }   
        
        //printing value after removing duplicate    
        for (int i=0; i<j; i++)  
           System.out.print(arr[i]+" "); 
	}
}

Output

Enter the size of array: 
5
Please give array input in sorted form 
Please give value for index 0 : 1
Please give value for index 1 : 2
Please give value for index 2 : 2
Please give value for index 3 : 3
Please give value for index 4 : 3
Array after removing duplicate values: 1 2 3

Program 2 : Removing duplicate array element in UnSorted Array

import java.util.*;
public class Main{
  public static void main (String[]args){
    Scanner sc = new Scanner (System.in);

    //taking array size and array element as input
      System.out.println ("Enter the size of array: ");
    int n = sc.nextInt ();
    int j = 0;
      Boolean[] mark = new Boolean[n];
    int arr[] = new int[n];
    for (int i = 0; i < n; i++){
	    System.out.print ("Please give value for index " + i + " : ");
	    arr[i] = sc.nextInt ();
	    mark[i] = true;
    }
    System.out.print ("Array after removing duplicate values: ");

    //complete logic to remove duplicate values in 
    //sorted and unsorted array
    for (int i = 0; i < n - 1; i++){
	    if (mark[i] == true)
	    {
	        for (j = i + 1; j < n - 1; j++)
	        {
		        if (arr[i] == arr[j])
		        {
		            mark[i] = false;
		        }
	        }
	    }
    }

    //printing value after removing duplicate    
    for (int i = 0; i < n; i++){
        if (mark[i] == true)
	    System.out.print (arr[i] + " ");
    }
    }
}

Output

Enter the size of array: 
6
Please give value for index 0 : 4
Please give value for index 1 : 2
Please give value for index 2 : 3
Please give value for index 3 : 4
Please give value for index 4 : 2
Please give value for index 5 : 1
Array after removing duplicate values: 3 4 2 1