Created
June 19, 2018 19:37
-
-
Save basekays/87aa08665ae02151ed0caa0bfee5fc99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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