Skip to content

Instantly share code, notes, and snippets.

@IAMIronmanSam
Created May 3, 2019 04:19
Show Gist options
  • Save IAMIronmanSam/23e23757c088c01f0e75ef32831c7a3b to your computer and use it in GitHub Desktop.
Save IAMIronmanSam/23e23757c088c01f0e75ef32831c7a3b to your computer and use it in GitHub Desktop.
Find possible sum pairs in array
function toSum(numArray,sum){
let pairs = [];
let hash = [];
for(let i = 0;i < numArray.length; i++){
let counterPart = sum - numArray[i];
if(hash.indexOf(counterPart) != -1){
pairs.push([numArray[i],counterPart]);
}
hash.push(numArray[i]);
}
return pairs;
}
toSum([1,2,3,4,5,6],7);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment