Skip to content

Commit a39deff

Browse files
authored
Create 1835-find-xor-sum-of-all-pairs-bitwise-and.js
1 parent 3cfe48c commit a39deff

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number[]} arr1
3+
* @param {number[]} arr2
4+
* @return {number}
5+
*/
6+
const getXORSum = function(arr1, arr2) {
7+
const bits = Array(32).fill(0)
8+
for (let v of arr2) {
9+
let pos = 0;
10+
while (v > 0) {
11+
if (v & 1) {
12+
bits[pos]++;
13+
}
14+
v = v >> 1;
15+
pos++;
16+
}
17+
}
18+
19+
let res = 0;
20+
21+
for (let v of arr1) {
22+
let pos = 0;
23+
let tmp = 0;
24+
while (v > 0) {
25+
if (v & 1) {
26+
if (bits[pos] % 2 == 1) {
27+
tmp |= (1 << pos);
28+
}
29+
}
30+
v = v >> 1;
31+
pos++;
32+
}
33+
34+
res ^= tmp;
35+
}
36+
37+
return res;
38+
};
39+

0 commit comments

Comments
 (0)