Skip to content

Instantly share code, notes, and snippets.

@abhilashsajeev
Created May 15, 2017 10:13
Show Gist options
  • Save abhilashsajeev/149a9925fed703696047b4ff07edb422 to your computer and use it in GitHub Desktop.
Save abhilashsajeev/149a9925fed703696047b4ff07edb422 to your computer and use it in GitHub Desktop.
Solution to Array pair sum problem in JavaScript
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