Skip to content

Commit cb52a4e

Browse files
add 136 solution[js]
1 parent b8b3efe commit cb52a4e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const singleNumber2 = function(nums){
2+
const map = {};
3+
for(let i = 0; i < nums.length; i++){
4+
if(map[nums[i]] === undefined){
5+
map[nums[i]] = 1;
6+
}else if(map[nums[i]] === 1){
7+
map[nums[i]]++;
8+
}
9+
}
10+
for(let key in map){
11+
if(map[key] === 1){
12+
return Number(key);
13+
}
14+
}
15+
}
16+
const singleNumber = function(nums){
17+
//XOR
18+
let result = 0;
19+
for(let i = 0; i < nums.length; i++){
20+
result = result ^ nums[i];
21+
}
22+
return result;
23+
24+
//or in es6
25+
//return nums.reduce((result, num) => result ^ num, 0);
26+
}

0 commit comments

Comments
 (0)