Leetcode 532 - K-diff Pairs in an Array

Note:

• If k === 0, we just need to know how many unique duplicate nums there are.
• If k !== 0, much like Two Sum, use set.has(n - k) and set.has(n + k) to find pairs.
• To avoid duplicates, we need to store checked pairs in another set.

Question:

Given an array of integers nums and an integer k, return the number of unique 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
• |nums[i] - nums[j]| == k

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

Example:

Code: