Leetcode 532 Solution

This article provides solution to leetcode question 532 (k-diff-pairs-in-an-array)

https://leetcode.com/problems/k-diff-pairs-in-an-array

Solution

class Solution:
    def findPairs(self, nums: List[int], k: int) -> int:
        cnt = collections.defaultdict(int)
        for num in nums:
            cnt[num] += 1

        ans = 0
        for v in cnt:
            if k != 0:
                if v + k in cnt:
                    ans += 1
            else:
                if cnt[v] > 1:
                    ans += 1

        return ans