Created
May 22, 2026 10:15
-
-
Save creativemind1/35dfa4466916e1d566689715bffb3263 to your computer and use it in GitHub Desktop.
Two Sum problem
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
| //Hash Method: | |
| function twoSum(nums, target) { | |
| const map = new Map(); // Stores value -> index | |
| for (let i = 0; i < nums.length; i++) { | |
| const complement = target - nums[i]; | |
| if (map.has(complement)) { | |
| return [map.get(complement), i]; // Found the two indices | |
| } | |
| map.set(nums[i], i); // Store the current value and its index | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment