Skip to content

Commit 18e00c9

Browse files
committed
Add Two Sum Problem
1 parent 5cdcdfb commit 18e00c9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

L-I/0009 TwoSum/TwoSum.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const TwoSum = (nums, target) => {
2+
numToIndex = {};
3+
4+
for (let i = 0; i < nums.length; i++) {
5+
if (target - nums[i] in numToIndex)
6+
return [numToIndex[target - nums[i]], i];
7+
8+
numToIndex[nums[i]] = i;
9+
}
10+
11+
return [];
12+
};
13+
14+
nums = [3, 2, 4];
15+
target = 6;
16+
17+
nums1 = [2, 7, 11, 15];
18+
target1 = 9;
19+
20+
nums2 = [3, 3];
21+
target2 = 6;
22+
23+
console.log(TwoSum(nums, target));
24+
console.log(TwoSum(nums1, target1));
25+
console.log(TwoSum(nums2, target2));

0 commit comments

Comments
 (0)