K-diff Pairs in an Array LeetCode solution in JavaScript

1
var findPairs = function (nums, k) {
2
nums.sort((a, b) => b - a);
3
const { length } = nums;
4
const hash = new Set();
5
let left = 0;
6
let right = 1;
7

8
while (left < length - 1) {
9
while (right < length) {
10
const diff = nums[left] - nums[right];
11

12
diff === k && hash.add(`${nums[left]},${nums[right]}`);
13
diff > k ? (right = length) : (right += 1);
14
}
15
left += 1;
16
right = left + 1;
17
}
18
return hash.size;
19
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0