Java | HashMap
For any number num, we need another number having atleast max(0,k-SetbitCount(num)) set bits in it. It doesn\'t matter where those bits are set at that number.\
For any number `num`, we need another number having atleast `max(0,k-SetbitCount(num))` set bits in it. It doesn\'t matter where those bits are set at that number.\nFor uniqueness we only count such unique numbers through set. \n```\nclass Solution {\n    public long countExcellentPairs(int[] nums, int k) {\n        HashMap<Integer,Set<Integer>> map = new HashMap<>();\n        for(int i : nums){\n            int x = Integer.bitCount(i);\n            map.putIfAbsent(x,new HashSet<>());\n            map.get(x).add(i);\n        }\n        long ans = 0;\n        HashSet<Integer> vis = new HashSet<>();\n        for(int i : nums){\n            if(vis.contains(i)) continue;\n            int need = Math.max(0,k-Integer.bitCount(i));\n            for(int key : map.keySet())   // runs at max 30 times\n\t\t\t\tif(key >= need) ans += (long) map.get(key).size();\n            vis.add(i);\n        }\n        return ans;\n    }\n}\n```
['Java']