Skip to content

Commit 7011143

Browse files
committed
Add another Java solution
1 parent a81eac0 commit 7011143

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public static int findUncoupled(int[] integers) {
3+
int allXored = 0;
4+
for (int i : integers) {
5+
allXored ^= i;
6+
}
7+
return allXored;
8+
}
9+
10+
public static int findUncoupledSet(int[] integers) {
11+
Set<Integer> seen = new HashSet<Integer>();
12+
13+
for (int i : integers) {
14+
if (seen.contains(i)) {
15+
seen.remove(i);
16+
} else {
17+
seen.add(i);
18+
}
19+
}
20+
21+
for (int uncoupled : seen) {
22+
// Will be only one
23+
return uncoupled;
24+
}
25+
26+
throw new IllegalArgumentException("Does not contain uncoupled integer.");
27+
}
28+
29+
public static void main(String[] args) {
30+
int[] ints = new int[] { 1, 1, 2, 2, 8 };
31+
System.out.println(findUncoupled(ints));
32+
System.out.println(findUncoupledSet(ints));
33+
}
34+
}

0 commit comments

Comments
 (0)