Skip to content

Instantly share code, notes, and snippets.

@creativemind1
Created May 22, 2026 10:15
Show Gist options
  • Select an option

  • Save creativemind1/35dfa4466916e1d566689715bffb3263 to your computer and use it in GitHub Desktop.

Select an option

Save creativemind1/35dfa4466916e1d566689715bffb3263 to your computer and use it in GitHub Desktop.
Two Sum problem
//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