Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.



Example 1:

Input: [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:

Input:[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:

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



Note:

The pairs (i, j) and (j, i) count as the same pair.
The length of the array won't exceed 10,000.
All the integers in the given input belong to the range: [-1e7, 1e7].

Source: leetcode | Difficulty: EASY
Tags: Data structures

Sorting

Amortized analysis

Starter code: class Solution:
    def findPairs(self, nums: List[int], k: int) -> int:
        

Solutions:
class Solution:

	def findPairs(self, nums, k):
		count = 0
		if k < 0:
			return count
		if k == 0:
			new_nums = collections.defaultdict(int)
			for i in nums:
				new_nums[i] += 1
			for value in new_nums:
				if new_nums[value] > 1:
					count += 1
			return count
		if k > 0:
			nums = set(nums)
			for i in nums:
				if i + k in nums:
					count += 1
			return count


class Solution:

	def findPairs(self, nums, k):
		import collections
		count = 0
		list_nums = set(nums)
		if k == 0:
			nums = collections.Counter(nums)
			for each in nums:
				if nums[each] > 1:
					count += 1
			return count
		elif k < 0:
			return 0
		elif k > 0:
			for i in list_nums:
				if i + k in list_nums:
					count += 1
			return count


class Solution:

	def findPairs(self, nums, k):
		dic = {}
		count = 0
		if k < 0:
			return 0
		if len(nums) == 1 or len(nums) == 0:
			return 0
		dic[nums[0]] = 1
		if k == 0:
			for i in range(1, len(nums)):
				if nums[i] in dic and dic[nums[i]] == 1:
					dic[nums[i]] += 1
					count += 1
				elif nums[i] not in dic:
					dic[nums[i]] = 1
				else:
					dic[nums[i]] += 1
		else:
			for i in range(1, len(nums)):
				if nums[i] - k in dic and nums[i] not in dic:
					count += 1
				if nums[i] + k in dic and nums[i] not in dic:
					count += 1
				dic[nums[i]] = 1
		return count


class Solution:

	def findPairs(self, nums, k):
		if k < 0:
			return 0
		(result, lookup) = (set(), set())
		for num in nums:
			if num + k in lookup:
				result.add(num)
			if num - k in lookup:
				result.add(num - k)
			lookup.add(num)
		return len(result)


class Solution:

	def findPairs(self, nums, k):
		if k < 0:
			return 0
		from collections import Counter
		c = Counter(nums)
		if k == 0:
			return sum([1 for (num, count) in list(c.items()) if count > 1])
		else:
			return sum([1 for (num, _) in list(c.items()) if num + k in c])


class Solution:

	def findPairs(self, nums, k):
		if k < 0:
			return 0
		unique = {}
		for num in nums:
			unique[num] = unique.get(num, 0) + 1
		res = 0
		for num in unique:
			if k == 0:
				if unique[num] > 1:
					res += 1
			elif num + k in unique:
				res += 1
		return res


class Solution:

	def findPairs(self, nums, k):
		result = 0
		c = collections.Counter(nums)
		for i in c:
			if k > 0 and i + k in c or (k == 0 and c[i] > 1):
				result += 1
		return result


class Solution:

	def findPairs(self, nums, k):
		num_count = dict()
		for n in nums:
			num_count[n] = num_count.get(n, 0) + 1
		total = 0
		for n in num_count:
			if k == 0 and num_count[n] > 1:
				total += 1
			elif k > 0 and n + k in num_count:
				total += 1
		return total


class Solution:

	def findPairs(self, nums, k):
		s = set(nums)
		total = 0
		if k < 0:
			return 0
		if k:
			for x in s:
				if x + k in s:
					total += 1
		else:
			counter = collections.Counter(nums)
			for (k, v) in counter.items():
				if v > 1:
					total += 1
		return total


class Solution:

	def findPairs(self, nums, k):
		if len(nums) < 2 or k < 0:
			return 0
		count = 0
		nums.sort()
		(i, j) = (0, 1)
		while i < len(nums) - 1 and j < len(nums):
			j = max(i + 1, j)
			d = nums[j] - nums[i]
			if d < k:
				j += 1
			elif d == k:
				count += 1
				while i + 1 < len(nums) and nums[i] == nums[i + 1]:
					i += 1
				i += 1
			else:
				i += 1
		return count


class Solution:

	def findPairs(self, nums, k):
		if k < 0:
			return 0
		pairs = {}
		cnt = 0
		for a in nums:
			if not a in pairs:
				if a + k in pairs and a not in pairs[a + k]:
					pairs[a + k].append(a)
					cnt += 1
				if a - k in pairs and a not in pairs[a - k]:
					pairs[a - k].append(a)
					cnt += 1
				pairs[a] = []
			else:
				if not a + k in pairs[a]:
					if a + k in pairs and a not in pairs[a + k]:
						pairs[a + k].append(a)
						cnt += 1
				if not a - k in pairs[a]:
					if a - k in pairs and a not in pairs[a - k]:
						pairs[a - k].append(a)
						cnt += 1
		return cnt