All examples

Two-sum (hashmap), .

Finds two indices summing to a target using a lookup object.

two-sum.js
function twoSum(nums, target) {
  const seen = {};
  for (let i = 0; i < nums.length; i++) {
    const need = target - nums[i];
    if (seen[need] !== undefined) return [seen[need], i];
    seen[nums[i]] = i;
  }
  return [];
}

twoSum([2, 7, 11, 15], 26);

What to watch

The seen object grows key by key; watch it find the complement and return both indices.

Reading two-sum (hashmap)on a page only gets you so far - code is a process, and the process happens at runtime where you can't normally see it. In NeonFlow, this exact program runs step by step: every call opens a frame, every branch picks a path, and every value moves through memory in front of you. That's the fastest way to actually understand how two-sum (hashmap) works.

Run two-sum (hashmap) yourself

Open NeonFlow, paste this code (or any of your own), and scrub through it one step at a time.

Open NeonFlow