-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleNumber2.java
More file actions
33 lines (28 loc) · 810 Bytes
/
SingleNumber2.java
File metadata and controls
33 lines (28 loc) · 810 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
/*
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99
Runtime: 6 ms, faster than 25.71% of Java online submissions
*/
class Solution {
public int singleNumber(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i = 0; i < nums.length; i ++) {
if(map.containsKey(nums[i])) {
map.put(nums[i], map.get(nums[i]) + 1);
} else {
map.put(nums[i], 1);
}
}
for(Integer key : map.keySet()) {
if(map.get(key) == 1) {
return key;
}
}
return -1;
}
}