Below is a working solution (very efficient) to the problem: “find index of array elements which sum together to equal target”
I have two questions:
1.How does this function rule out cases where an element of an array is added to itself to equal the target?
- How does “map.set(nums[i], i);” end up returning an array with the index of the two elements separated by a comma?
function bebo(nums, target) {
let map = new Map;
for (var i = 0; i < nums.length; i++) {
let complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i]
}
map.set(nums[i], i);
}
}
bebo([2,11,7,15],9)
Thanks in advance : )