-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC136.java
More file actions
37 lines (31 loc) · 857 Bytes
/
Copy pathLC136.java
File metadata and controls
37 lines (31 loc) · 857 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
class LC136 {
public static void main(String args[]) {
Solution singleNumber = new Solution();
int[] testCase = {1,2,2,3,4,1,3};
System.out.println(singleNumber.singleNumber(testCase));
}
}
/*
class Solution {
public int singleNumber(int[] nums) {
HashMap<Integer, Integer> countTable = new HashMap<>();
for(int num : nums) {
countTable.put(num, countTable.getOrDefault(num, 0) + 1);
}
for(Integer key : countTable.keySet()) {
if(countTable.get(key) == 1) return (int) key;
}
return 0;
}
}
*/
class Solution {
// copilot suggested method using XOR instead
public int singleNumber(int[] nums) {
int result = 0;
for(int num : nums) {
result ^= num;
}
return result;
}
}