Programming Examples

Java program to delete duplicate elements from an array


Write a Java Program which accept an Array from user and delete all duplicate elements from and print the array.

import java.util.*;
class DeleteDuplicate
{
    public static void main(String arr[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the size of Array : ");
        int size=sc.nextInt();
        int num[]=new int[size];
        System.out.println("Enter "+size+" Elements for Array :");
        for(int i=0;i        {
            num[i]=sc.nextInt();
        }
        for(int i=0;i        {
            for(int j=i+1;j            {
                if(num[i]==num[j])
                {
                    size=size-1;
                    for(int k=j;k                    {
                        num[k]=num[k+1];
                    }
                    j--;
                }
            }
        }
        System.out.println("Array after delete duplicate elements :");
        for(int i=0;i        {
           System.out.println(num[i]+" ");
        }
       
    }
}
Output

Enter the size of Array : 

10

Enter 10 Elements for Array :

2

2

3

3

4

4

3

4

5

2

Array after delete duplicate elements :

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz