C++, self-explanatory one-pass O(n) use hash map
cpp\nint findPairs(vector<int>& nums, int k) {;\n    unordered_map<int, int> freq;\n    int ans = 0;\n    for (int n : nums) {\n        if (k == 0) {\n         
```cpp\nint findPairs(vector<int>& nums, int k) {;\n    unordered_map<int, int> freq;\n    int ans = 0;\n    for (int n : nums) {\n        if (k == 0) {\n            if (freq[n] == 1) ++ans; // only count once\n        } else if (!freq.count(n)) { // only count when the first time n appears\n            if (freq.count(n - k)) ++ans;\n            if (freq.count(n + k)) ++ans;\n        }\n        ++freq[n];\n    }\n    return ans;\n}\n```
['C']