-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleNumber.java
46 lines (35 loc) · 1.15 KB
/
SingleNumber.java
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
package solutions;
import java.util.HashMap;
import java.util.Map;
// [Problem] https://leetcode.com/problems/single-number
class SingleNumber {
// test
public static void main(String[] args) {
SingleNumber solution = new SingleNumber();
int[] input = new int[]{4, 1, 2, 1, 2};
int expectedOutput = 4;
int actualOutput = solution.singleNumber(input);
System.out.println("Test passed? " + (expectedOutput == actualOutput));
}
// HashMap solution - O(N) Time, O(N) Space
private int singleNumberHashmap(int[] nums) {
Map<Integer, Integer> numOccurrence = new HashMap<>();
for (int num : nums) {
numOccurrence.put(num, numOccurrence.containsKey(num) ? numOccurrence.get(num) + 1 : 1);
}
for (int num : numOccurrence.keySet()) {
if (numOccurrence.get(num) == 1) {
return num;
}
}
return 0;
}
// XOR bit manipulation solution - O(N) Time, O(1) Space
private int singleNumber(int[] nums) {
int xor = 0;
for (int num : nums) {
xor ^= num;
}
return xor;
}
}