Skip to content

Instantly share code, notes, and snippets.

@basekays
Created June 19, 2018 19:37
Show Gist options
  • Select an option

  • Save basekays/87aa08665ae02151ed0caa0bfee5fc99 to your computer and use it in GitHub Desktop.

Select an option

Save basekays/87aa08665ae02151ed0caa0bfee5fc99 to your computer and use it in GitHub Desktop.
//input = array, number
//output = array
//test case
//[2, 7, 11, 15], 13
//[0, 2]
function twoSum (array, number) {
var solutionObject = {};
for (let i = 0; i < array.length; i++) {
const target = number - array[i];
if (target in solutionObject) {
return [solutionObject[target], i];
} else {
solutionObject[array[i]] = i;
}
}
}
twoSum([4, 5, 10], 14);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment