# 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 theirabsolute differenceis`k`

.

**Example:**

1 | Input: [3, 1, 4, 1, 5], k = 2 |

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

1 | public int findPairs(int[] nums, int k) { |

**Optimization:**

1 | public int findPairs(int[] nums, int k) { |

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

Comment