DSA · Arrays & hashing
The problem
Given an array nums and a number target, return the indices of the two numbers that add up
to target.
The obvious solution checks every pair:
function twoSum(nums: number[], target: number) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) return [i, j];
}
}
return null;
}