K-diff Pairs in an Array

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

# Solution

Could also do brute force or two pointers (needs sorting), but the following is linear time.

# HashMap

from collections import Counter

class Solution:
    def findPairs(self, nums: List[int], k: int) -> int:
        map = Counter(nums)
        res = 0
        if k == 0:
            for i in map:
                if map[i] > 1: res += 1
        elif k > 0:
            for i in map:
                if map[i+k] >= 1: res += 1
        return res
1
2
3
4
5
6
7
8
9
10
11
12
13