← Practice · Hash map

Two Sum Easy

Given an array of integers nums and an integer target, return the indices of the two numbers that add up to target, smaller index first. Exactly one valid pair exists and you may not use the same element twice.

Examples

  • nums = [2,7,11,15], target = 9 -> [0,1]
  • nums = [3,3], target = 6 -> [0,1]

Define a function named twoSum in JavaScript.

00:00▶ Visualize

JavaScript · Tab indents · Ctrl/⌘+Enter runs the tests