Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions 532. K-diff Pairs in an Array.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
var findPairs = function(nums, k) {
if(nums.length === 0 || k < 0) {
return 0;
}
var dict = {};
var count = 0;
if (nums.length === 0 || k < 0) {
return 0;
}
var dict = {};
var count = 0;

nums.sort(function(a,b){ return a - b });
for(var i = 0; i < nums.length; i++) {
var number = nums[i];
dict[number] = (dict[number] === undefined)? 1 : dict[number] += dict[number];
}
for(var numb in dict) {
numb = parseInt(numb);
if(k === 0) {
if(dict[numb] > 1) {
count++;
}
} else if(dict[numb + k] !== undefined){
count++;
//nums.sort(function(a,b){ return a - b });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete this line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

for (var i = 0; i < nums.length; i++) {
var number = nums[i];
dict[number] = (dict[number] === undefined) ? 1 : dict[number] += dict[number];
}
for (var numb in dict) {
numb = parseInt(numb);
if (k === 0) {
if (dict[numb] > 1) {
count++;
}
} else if (dict[numb + k] !== undefined) {
count++;
}
}
}
return count;
};
return count;
};