diff --git a/two-sum/yoonthecoder.js b/two-sum/yoonthecoder.js new file mode 100644 index 000000000..f37002549 --- /dev/null +++ b/two-sum/yoonthecoder.js @@ -0,0 +1,14 @@ +var twoSum = function (nums, target) { + const map = new Map(); + + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i]; + if (map.has(complement)) { + return [map.get(complement), i]; + } + map.set(nums[i], i); + } +}; + +// Time complexity: O(n) +// Space complexity: O(n) - hash map storage