We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 83e0596 commit 8333b7aCopy full SHA for 8333b7a
137-single-number-ii.js
@@ -28,3 +28,29 @@ const singleNumber = (nums)=> {
28
}
29
return one;
30
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