Problem

Given an array of integers nums and an integer k, return the number ofunique k-diff pairs in the array.

A k-diff pair is an integer pair (nums[i], nums[j]), where the following are true:

  • 0 <= i, j < nums.length
  • i != j
  • |nums[i] - nums[j]| == k

Notice that |val| denotes the absolute value of val.

Examples

Example 1

1
2
3
4
Input: nums = [3,1,4,1,5], k = 2
Output: 2
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of **unique** pairs.

Example 2

1
2
3
Input: nums = [1,2,3,4,5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).

Example 3

1
2
3
Input: nums = [1,3,1,5,4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).

Constraints

  • 1 <= nums.length <= 10^4
  • -10^7 <= nums[i] <= 10^7
  • 0 <= k <= 10^7

Solution

Method 1 – Hash Map Counting

Intuition

To count unique k-diff pairs, we can use a hash map to count occurrences. For k > 0, for each unique number, check if num + k exists. For k = 0, count numbers that appear more than once.

Approach

  1. If k < 0, return 0 (no valid pairs).
  2. Count occurrences of each number using a hash map.
  3. If k == 0:
    • For each number, if its count > 1, increment the answer.
  4. If k > 0:
    • For each unique number, if num + k exists in the map, increment the answer.
  5. Return the answer.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
public:
    int findPairs(vector<int>& nums, int k) {
        if (k < 0) return 0;
        unordered_map<int, int> cnt;
        for (int n : nums) cnt[n]++;
        int ans = 0;
        if (k == 0) {
            for (auto& [n, c] : cnt) if (c > 1) ans++;
        } else {
            for (auto& [n, _] : cnt) if (cnt.count(n + k)) ans++;
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
func findPairs(nums []int, k int) int {
    if k < 0 { return 0 }
    cnt := map[int]int{}
    for _, n := range nums { cnt[n]++ }
    ans := 0
    if k == 0 {
        for _, c := range cnt { if c > 1 { ans++ } }
    } else {
        for n := range cnt { if _, ok := cnt[n+k]; ok { ans++ } }
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public int findPairs(int[] nums, int k) {
        if (k < 0) return 0;
        Map<Integer, Integer> cnt = new HashMap<>();
        for (int n : nums) cnt.put(n, cnt.getOrDefault(n, 0) + 1);
        int ans = 0;
        if (k == 0) {
            for (int c : cnt.values()) if (c > 1) ans++;
        } else {
            for (int n : cnt.keySet()) if (cnt.containsKey(n + k)) ans++;
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    fun findPairs(nums: IntArray, k: Int): Int {
        if (k < 0) return 0
        val cnt = mutableMapOf<Int, Int>()
        for (n in nums) cnt[n] = cnt.getOrDefault(n, 0) + 1
        var ans = 0
        if (k == 0) {
            for (c in cnt.values) if (c > 1) ans++
        } else {
            for (n in cnt.keys) if (cnt.containsKey(n + k)) ans++
        }
        return ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
    def findPairs(self, nums: list[int], k: int) -> int:
        if k < 0:
            return 0
        from collections import Counter
        cnt = Counter(nums)
        ans = 0
        if k == 0:
            for v in cnt.values():
                if v > 1:
                    ans += 1
        else:
            for n in cnt:
                if n + k in cnt:
                    ans += 1
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
use std::collections::HashMap;
impl Solution {
    pub fn find_pairs(nums: Vec<i32>, k: i32) -> i32 {
        if k < 0 { return 0; }
        let mut cnt = HashMap::new();
        for n in nums { *cnt.entry(n).or_insert(0) += 1; }
        let mut ans = 0;
        if k == 0 {
            for &c in cnt.values() { if c > 1 { ans += 1; } }
        } else {
            for &n in cnt.keys() { if cnt.contains_key(&(n + k)) { ans += 1; } }
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    findPairs(nums: number[], k: number): number {
        if (k < 0) return 0;
        const cnt = new Map<number, number>();
        for (const n of nums) cnt.set(n, (cnt.get(n) ?? 0) + 1);
        let ans = 0;
        if (k === 0) {
            for (const c of cnt.values()) if (c > 1) ans++;
        } else {
            for (const n of cnt.keys()) if (cnt.has(n + k)) ans++;
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of nums. We count and check each number once.
  • 🧺 Space complexity: O(n), for the hash map storing counts.