Created
May 15, 2017 10:13
-
-
Save abhilashsajeev/149a9925fed703696047b4ff07edb422 to your computer and use it in GitHub Desktop.
Solution to Array pair sum problem in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pairSum = (arr, k)=>{ | |
if(arr.length < 2){ | |
return | |
} | |
// sets for tracting | |
let seen = new Set(); | |
let output = new Set(); | |
for (let num of arr){ | |
let target = k - num; | |
if(!seen.has(target)){ | |
seen.add(num) | |
} else { | |
let obj = [Math.min(num, target),Math.max(num, target)] | |
output.add(obj) | |
console.log(obj) | |
} | |
} | |
return output.size | |
} | |
console.log(pairSum([1,3,2,2], 4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment