-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit-manipulation-lonely-integer.java
More file actions
66 lines (49 loc) · 1.52 KB
/
bit-manipulation-lonely-integer.java
File metadata and controls
66 lines (49 loc) · 1.52 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Consider an array of integers, , where all but one of the integers occur in pairs. In other words, every element in occurs exactly twice except for one unique element.
Given , find and print the unique element.
Input Format
The first line contains a single integer, , denoting the number of integers in the array.
The second line contains space-separated integers describing the respective values in .
Constraints
It is guaranteed that is an odd number.
, where .
Output Format
Print the unique number that occurs only once in on a new line.
Sample Input 0
1
1
Sample Output 0
1
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static int lonelyInteger(int[] a) {
Map<Integer, Boolean> unique = new HashMap<Integer, Boolean>();
for (int i = 0; i < a.length; i++) {
if (unique.containsKey(a[i])) {
unique.put(a[i], false);
} else {
unique.put(a[i], true);
}
}
for (int i = 0; i < a.length; i++) {
if (unique.get(a[i]) == true) {
return a[i];
}
}
return -1;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[] = new int[n];
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
}
System.out.println(lonelyInteger(a));
}
}