Number of Excellent Pairs LeetCode solution in JavaScript

1
var countExcellentPairs = function (nums, k) {
2
const map = new Map();
3
const set = new Set();
4
const l = nums.length;
5
let res = 0;
6
for (let num of nums) {
7
let temp = num.toString(2).split("1").length - 1;
8
if (!map.has(temp)) {
9
map.set(temp, new Set([num]));
10
} else {
11
map.get(temp).add(num);
12
}
13
}
14

15
for (let num of nums) {
16
let temp = num.toString(2).split("1").length - 1;
17
if (!set.has(num)) {
18
let gap = Math.max(0, k - temp);
19
for (let key of map.keys()) {
20
if (key >= gap) {
21
res += map.get(key).size;
22
}
23
}
24
set.add(num);
25
} else {
26
continue;
27
}
28
}
29
return res;
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0