Skip to content

Commit 38feb56

Browse files
committed
commit
1 parent 8c9c53f commit 38feb56

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

137. Single Number II.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int singleNumber(int[] nums) {
3+
// Create a hashmap to track numbers
4+
Map<Integer, Integer> map = new HashMap();
5+
6+
// Push all elements into hashmap
7+
for (int num : nums) {
8+
// Put the value as how many number found in array
9+
map.put(num, map.getOrDefault(num, 0) + 1);
10+
}
11+
12+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
13+
// If the key value is 1 means, the number only appeared once in the array
14+
if (entry.getValue() == 1) {
15+
return entry.getKey();
16+
}
17+
}
18+
19+
// Not found
20+
return -1;
21+
}
22+
}

0 commit comments

Comments
 (0)