Created
July 5, 2023 16:00
-
-
Save nickforce/7f8acd94cab8f5850ddf90652c8fa13a to your computer and use it in GitHub Desktop.
Two Sum (leetcode)
This file contains 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 numObj = {} | |
for(i = 0; i < nums.length; i++) { | |
let other = target - nums[i]; | |
if(numObj[other] !== undefined) { | |
return [numObj[other], i]; | |
} | |
numObj[nums[i]] = i; | |
} | |
}; | |
console.log(twoSum([3,3], 6)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment