Skip to content

Commit 4716598

Browse files
authored
Create 532-k-diff-pairs-in-an-array.js
1 parent 5518955 commit 4716598

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

532-k-diff-pairs-in-an-array.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const findPairs = function(nums, k) {
7+
if(k < 0) return 0
8+
let count = 0
9+
const hash = {}
10+
for(let el of nums) {
11+
if (hash.hasOwnProperty(el)) {
12+
if(k === 0 && hash[el] === 1) {
13+
count++
14+
}
15+
hash[el] += 1
16+
} else {
17+
if (hash.hasOwnProperty(el - k)) {
18+
count++
19+
}
20+
if (hash.hasOwnProperty(el + k)) {
21+
count++
22+
}
23+
hash[el] = 1
24+
}
25+
}
26+
27+
return count
28+
};

0 commit comments

Comments
 (0)