Skip to content

Commit 8333b7a

Browse files
authored
Update 137-single-number-ii.js
1 parent 83e0596 commit 8333b7a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

137-single-number-ii.js

+26
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,29 @@ const singleNumber = (nums)=> {
2828
}
2929
return one;
3030
}
31+
32+
// another
33+
/**
34+
* @param {number[]} nums
35+
* @return {number}
36+
*/
37+
const singleNumber = (nums)=> {
38+
// Initialize result
39+
let result = 0;
40+
let x, sum;
41+
const n = nums.length
42+
// Iterate through every bit
43+
for (let i = 0; i < 32; i++) {
44+
// Find sum of set bits at ith position in all
45+
// array elements
46+
sum = 0;
47+
x = (1 << i);
48+
for (let j = 0; j < n; j++ ) {
49+
if (nums[j] & x) sum++;
50+
}
51+
// The bits with sum not multiple of 3, are the
52+
// bits of element with single occurrence.
53+
if (sum % 3) result |= x;
54+
}
55+
return result;
56+
}

0 commit comments

Comments
 (0)