-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique0.java
More file actions
37 lines (33 loc) · 958 Bytes
/
Copy pathunique0.java
File metadata and controls
37 lines (33 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class unique0 {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the length of array");
int n=sc.nextInt();
int[] arr=new int[n];
System.out.println("Enter the elements of an array");
int ans=-1;
int[] visited=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
if(visited[i]==0) {
int cnt = 1;
for (int j = i + 1; j < n; j++) {
if (arr[j] == arr[i])
{cnt++;
visited[j] = 1;}
}
if(cnt==1){ans=arr[i];
break;}
}
}
System.out.println("unique element:" + ans);
}
}