Number of Excellent Pairs LeetCode solution in JavaScript
1var countExcellentPairs = function (nums, k) {2const map = new Map();3const set = new Set();4const l = nums.length;5let res = 0;6for (let num of nums) {7let temp = num.toString(2).split("1").length - 1;8if (!map.has(temp)) {9map.set(temp, new Set([num]));10} else {11map.get(temp).add(num);12}13}1415for (let num of nums) {16let temp = num.toString(2).split("1").length - 1;17if (!set.has(num)) {18let gap = Math.max(0, k - temp);19for (let key of map.keys()) {20if (key >= gap) {21res += map.get(key).size;22}23}24set.add(num);25} else {26continue;27}28}29return res;30};