-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleNumber.java
More file actions
38 lines (28 loc) · 815 Bytes
/
SingleNumber.java
File metadata and controls
38 lines (28 loc) · 815 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
34
35
36
37
38
package kent.alg.leetcode;
import java.util.HashMap;
import org.junit.jupiter.api.Test;
public class SingleNumber {
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] )) {
int counter = map.get(nums[i]) + 1;
map.put(nums[i], counter);
}
else {
map.put(nums[i], 1);
}
}
for(Object key : map.keySet()) {
int result = map.get(key);
if(result == 1)
return (int) key;
}
return -1;
}
@Test
public void Test() {
int[] nums = {4,1,1,0,0,2,1,2};
System.out.println(singleNumber(nums));
}
}