We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8c9c53f commit 38feb56Copy full SHA for 38feb56
137. Single Number II.java
@@ -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