Java O(n) solution with hashmap
\nclass Solution {\n    public int findPairs(int[] nums, int k) {\n        int kDiffs = 0;\n        Map<Integer, Integer> counter = new HashMap<>();\n        \n
```\nclass Solution {\n    public int findPairs(int[] nums, int k) {\n        int kDiffs = 0;\n        Map<Integer, Integer> counter = new HashMap<>();\n        \n        for (int n: nums) {\n            int countOfN = counter.getOrDefault(n, 0);\n            countOfN++;\n            counter.put(n, countOfN);\n            \n            if (k == 0) {\n                if (countOfN == 2) kDiffs++;\n            } else if (countOfN == 1) {\n                if (counter.containsKey(n - k)) kDiffs++;\n                if (counter.containsKey(n + k)) kDiffs++;           \n            }\n        }\n        \n        return kDiffs;\n    }\n}\n\n```
[]