Java HashMap
\n    public int findPairs(int[] nums, int k) {\n        if (k < 0) return 0;\n        Map<Integer, Integer> map = new HashMap<>();\n        int res = 0;\n     
```\n    public int findPairs(int[] nums, int k) {\n        if (k < 0) return 0;\n        Map<Integer, Integer> map = new HashMap<>();\n        int res = 0;\n        for(int i : nums) {\n            if (map.containsKey(i)) {\n                if (k == 0 && map.get(i) == 1) {\n                    map.put(i, map.get(i) + 1);\n                    res++;\n                }\n                continue;\n            }\n            res += map.getOrDefault(i + k, 0);\n            res += map.getOrDefault(i - k, 0);\n            map.put(i, 1);\n        }\n        return res;\n    }\n```
[]