Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Javascript/twoSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Two Sum Problem Solution in JavaScript
var twoSum = function(nums, target) {
const numMap = new Map(); // Create a hashmap to store the numbers and their indices

for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i]; // Find the complement

if (numMap.has(complement)) {
return [numMap.get(complement), i]; // Return the indices if complement exists in the map
}

numMap.set(nums[i], i); // Add the current number and its index to the map
}

throw new Error("No two sum solution found!");
};

// Example Usage:
console.log(twoSum([2, 7, 11, 15], 9)); // Output: [0, 1]