---
# System prepended metadata

title: k-diff

---

# k-diff

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: (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: (1, 2), (2, 3), (3, 4) and (4, 5).
```

Example 3:

```
Input: [1, 3, 1, 5, 4], k = 0
Output: 1
Explanation: (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].
    

## Solution

```python=

def k_diff(array, k): # input is an array and defination of k-diff
    
    index = 0
    length = len(array)
    result = [] # result is a list that is used to record the solution
    for index in range(length)
        j = index + 1
        for j in range(length)
            if abs(array[j] - array[i]) == k # check if absolute diff is equal to k
                if (array[i], array[j]) not in result # if so, check whether or not it is a new solution
                    result.expand((array[i], array[j])) # if it is an acceptable solution, add to the result
    
    return len(result) # use len() to return the number of pairs in the result list
                    
    
```