Skip to content

Commit a22a964

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

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,13 @@ const findPairs = function(nums, k) {
77
if(k < 0) return 0
88
let count = 0
99
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
10+
for(let i = 0; i < nums.length; i++) {
11+
hash[nums[i]] = i
12+
}
13+
for(let i = 0; i < nums.length; i++) {
14+
if(hash.hasOwnProperty(nums[i] + k) && hash[nums[i] + k] !== i) {
15+
count++
16+
delete hash[nums[i] + k]
2417
}
2518
}
2619

0 commit comments

Comments
 (0)