Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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);
Add Comment
Please, Sign In to add comment