forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_137.java
32 lines (25 loc) · 835 Bytes
/
_137.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
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
/**137. Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
*/
public class _137 {
public int singleNumber(int[] nums) {
Map<Integer, Integer> map = new HashMap();
for(int i : nums){
map.put(i, map.getOrDefault(i, 0) + 1);
}
for(int key : map.keySet()){
if(map.get(key) != 3) return key;
}
return 0;
}
public static void main(String...strings){
int[] nums = new int[]{2,2,3,2};
_137 test = new _137();
System.out.println(test.singleNumber(nums));
}
}