Programming Examples

Java program to input n elements in array and print unique elements


Write a java program which accept n Elements in array and print unique Element of an array.

Solution:

import java.util.*;
class UniqueElement
{
    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<size;i++)
        {
            num[i]=sc.nextInt();
        }
        boolean flag; 
        System.out.println("Array Elements with repeat-ion (unique)");
        for(int i=0;i<size;i++)
        {
            flag=true;
            for(int b=i-1;b>=0;b--)
            {
                if(num[i]==num[b])
                {
                    flag=false;
                    break;
                }
            }
            if(flag==true)
            {
                System.out.print(num[i]+" ");
            }
        }       
    }
}
Output

Enter the size of Array : 

8

Enter 8 Elements for Array :

2

3

4

3

4

5

6

7

Array Elements with repeat-ion (unique)

2 3 4 5 6 7 

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