Skip to content

Commit 031e04b

Browse files
Merge pull request chihungyu1116#13 from ignacio-chiazzo/single-number-III
added single number III solution
2 parents 41d9f6f + 28256c4 commit 031e04b

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

260. Single Number III.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var singleNumber = function(nums) {
6+
if(nums === null || nums.length <= 2) {
7+
return nums;
8+
}
9+
10+
var xor = nums[0];
11+
for(var i = 1; i < nums.length; i++) {
12+
xor ^= nums[i];
13+
}
14+
15+
var exp = 1;
16+
while(!(exp & xor)) {
17+
exp = exp * 2;
18+
}
19+
console.log(exp);
20+
var xorBit0 = 0;
21+
var xorBit1 = 0;
22+
23+
for(var j = 0; j < nums.length; j++) {
24+
if(exp & nums[j]){
25+
xorBit1 ^= nums[j];
26+
console.log("with 1: " + nums[j]);
27+
} else {
28+
console.log("with 0: " + nums[j]);
29+
xorBit0 ^= nums[j];
30+
}
31+
}
32+
33+
return [xorBit0, xorBit1];
34+
};

0 commit comments

Comments
 (0)