Skip to content

Instantly share code, notes, and snippets.

@jchiatt
Created October 29, 2020 15:11
Show Gist options
  • Save jchiatt/ecc765883e40f2814eebc6a42954cf90 to your computer and use it in GitHub Desktop.
Save jchiatt/ecc765883e40f2814eebc6a42954cf90 to your computer and use it in GitHub Desktop.
function twoNumberSum(array, targetSum) {
if ( !Array.isArray(array) || !array.length ) throw new Error('You must provide an array of values.');
const result = [];
const numberMap = {};
for ( let i = 0; i < array.length; i++ ) {
const diff = targetSum - array[i];
console.log(diff)
if ( numberMap[diff] ) {
result.push(array[i], diff);
break;
}
numberMap[array[i]] = true;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment