1. Two Sum
Difficulty: EASY
Problem description can be found here.
Approach
Section titled “Approach”Basically use a HashMap. The key is to store the difference from each number in the array from the target.
If the difference exist, we basically found our solution.
Solution
Section titled “Solution”function twoSum(nums: number[], target: number): number[] { const hash: Record<number, number> = {}; const output: number[] = [];
for (let i = 0; i < nums.length; i++) { const difference = target - nums[i];
if (difference in hash) { output.push(i); output.push(hash[difference]); }
hash[nums[i]] = i; }
return output;}