Created
January 18, 2020 10:39
-
-
Save dashsaurabh/6269344fe0cf518e67afc6aa2dc42e92 to your computer and use it in GitHub Desktop.
Find the Indices of Two Numbers From an Array Whose Sum is Equal to the Target
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
| /** | |
| * @param {number[]} nums | |
| * @param {number} target | |
| * @return {number[]} | |
| */ | |
| var twoSum = function(nums, target) { | |
| let lookUp = {} | |
| for(let i = 0; i < nums.length; i++) { | |
| let complement = target - nums[i]; | |
| if(lookUp[complement] !== undefined){ | |
| return [lookUp[complement], i] | |
| } | |
| lookUp[nums[i]] = i; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment