We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 39bb2ba commit 0ab6459Copy full SHA for 0ab6459
532. K-diff Pairs in an Array.js
@@ -0,0 +1,24 @@
1
+var findPairs = function(nums, k) {
2
+ if(nums.length === 0 || k < 0) {
3
+ return 0;
4
+ }
5
+ var dict = {};
6
+ var count = 0;
7
+
8
+ nums.sort(function(a,b){ return a - b });
9
+ for(var i = 0; i < nums.length; i++) {
10
+ var number = nums[i];
11
+ dict[number] = (dict[number] === undefined)? 1 : dict[number] += dict[number];
12
13
+ for(var numb in dict) {
14
+ numb = parseInt(numb);
15
+ if(k === 0) {
16
+ if(dict[numb] > 1) {
17
+ count++;
18
19
+ } else if(dict[numb + k] !== undefined){
20
21
22
23
+ return count;
24
+};
0 commit comments