function twoSum(nums, target) {
const need = new Map();
for (let i = 0; i < nums.length; i++) {
const x = nums[i];
if (need.has(x)) return [need.get(x), i];
need.set(target - x, i);
}
return null;
}
twoSum([2, 7, 11, 15], 9);
Hash Map за O(n) — канонический ответ.