Java O(n) solution - one Hashmap, easy to understand
\npublic class Solution {\n    public int findPairs(int[] nums, int k) {\n        if (nums == null || nums.length == 0 || k < 0)   return 0;\n        \n        
```\npublic class Solution {\n    public int findPairs(int[] nums, int k) {\n        if (nums == null || nums.length == 0 || k < 0)   return 0;\n        \n        Map<Integer, Integer> map = new HashMap<>();\n        int count = 0;\n        for (int i : nums) {\n            map.put(i, map.getOrDefault(i, 0) + 1);\n        }\n        \n        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {\n            if (k == 0) {\n                //count how many elements in the array that appear more than twice.\n                if (entry.getValue() >= 2) {\n                    count++;\n                } \n            } else {\n                if (map.containsKey(entry.getKey() + k)) {\n                    count++;\n                }\n            }\n        }\n        \n        return count;\n    }\n}\n```
[]