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

Let us just use counter and count frequency of each number in our array. We can have two options:

  1. k > 0, it means, that for each unique number i we are asking if number i+k also in table.
  2. k = 0, it means, that we are looking for pairs of equal numbers, so just check each frequency.

Complexity: time and space complexity is O(n), because we traverse our array twice: first time to create counter and second to find res.

class Solution:
    def findPairs(self, nums, k):
        count = Counter(nums)
        if k > 0:
            res = sum([i + k in count for i in count])
        else:
            res = sum([count[i] > 1 for i in count])
        return res

If you like the solution, you can upvote it on leetcode discussion section: Problem 0532