Complete the code to declare the hash map for storing numbers and their indices.
const numMap = new Map<number, number>[1];The Map constructor is called with parentheses () to create a new empty map.
Complete the code to check if the complement exists in the map.
if (numMap.[1](complement)) {
get which returns the value, not a boolean.set which adds or updates a key.The has method checks if a key exists in the Map.
Fix the error in the code to add the current number and its index to the map.
numMap.[1](nums[i], i);get which retrieves a value.push which is for arrays, not maps.The set method adds or updates a key-value pair in the Map.
Fill both blanks to complete the return statement with indices of the two numbers.
return [numMap.get([1]), [2]];
The first index is found by looking up the complement in the map, the second is the current index i.
Fill all three blanks to complete the Two Sum function using a hash map.
function twoSum(nums: number[], target: number): number[] {
const numMap = new Map<number, number>[1];
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (numMap.[2](complement)) {
return [numMap.get(complement), [3]];
}
numMap.set(nums[i], i);
}
return [];
}The map is initialized with (), has checks for complement, and i is the current index returned.
