0% found this document useful (0 votes)
9 views2 pages

Array Java 5

The document describes a program to print all unique elements in an array. The program takes user input for the number of elements and values of the array. It then prints the non-repeated elements and number of unique elements.

Uploaded by

Anas Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Array Java 5

The document describes a program to print all unique elements in an array. The program takes user input for the number of elements and values of the array. It then prints the non-repeated elements and number of unique elements.

Uploaded by

Anas Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

4. Write a program to print all unique elements in an array.

PROGRAM:
import java.util.Scanner;
public class lab6da{
public static void main(String[] args) {
int n, flag = 0, count = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for(int i = 0; i < n; i++){
a[i] = s.nextInt();}
System.out.print("Non repeated elements are:");
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i != j){
if(a[i] != a[j]){
flag = 1;}
else{
flag = 0;
break;}}}
if(flag == 1){
count++;
System.out.print(a[i]+" ");}}
System.out.println("");
System.out.println("Number of non repeated elements are:"+count);}}

OUTPUT:

You might also like