# 532. K-diff Pairs in an Array

Reference: LeetCode
Difficulty: Easy

## Problem

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:

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].

## Analysis

### Hash Map

Note:

• If k == 0, we only consider duplicates.
• Since (i, j) and (j, i) are counted as 2, we should divide the result by 2 before returning it.
• Actually, we don’t need to do it that way.

Optimization:

Time: $O(N)$
Space: $O(N)$

Comment
Junhao Wang
a software engineering cat
Info
Article :
50
Total Count :
82.4k
UV :
PV :
Last Push :