SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Algorithm Patterns OverviewFrequency CountersTwo SumValid AnagramGroup AnagramsFrequency Counters and Hash-Map LookupTwo PointersValid PalindromeTwo Sum IIContainer With Most WaterTwo-Pointer ReasoningSliding WindowMaximum Average Subarray ILongest Substring Without Repeating CharactersMinimum Size Subarray SumSliding-Window Reasoning
Algorithm Patterns

Two Sum

Apply hash-map lookups to solve Two Sum and verify the implementation with test cases.

Try It Yourself

Use the editor below to find two array positions whose values add up to the target.

Scan from left to right

Think about what information to remember as you scan. Handle duplicate values, negative numbers, and pairs far apart in the array.

Submit your implementation when it passes the examples.

Two Sum

easyHash Map · Arrays20 minLC 1
Problem

Given an array of integers nums and an integer target, return the indices of the two different elements whose values add up to target.

Exactly one valid answer exists. Return the two indices in increasing order.

Example 1:

Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]
Explanation: 2 and 7 add up to 9. Their zero-based indices are 0 and 1.

Example 2:

Input: nums = [3, 2, 4], target = 6
Output: [1, 2]
Explanation: 2 and 4 add up to 6. Their zero-based indices in the original array are 1 and 2.

Constraints

  • 2 ≤ nums.length ≤ 10,000
  • Exactly one valid pair exists.
0 attempts

Test results

Results appear here after a run.

Solution

For target 9, store the value 2 at index 0. At index 1, the value 7 needs 2, giving positions 0 and 1.

Store each value's index as you scan the array. Before storing the current value, calculate the complement needed to reach the target and check whether that complement has already appeared.

Checking first prevents the same element from being used twice. In the figure the two values are 2 and 7. Return their zero-based indices, [0, 1], in increasing order.

function twoSum(nums, target) {
  const indexByValue = new Map();

  for (let index = 0; index < nums.length; index++) {
    const complement = target - nums[index];

    if (indexByValue.has(complement)) {
      return [indexByValue.get(complement), index];
    }

    indexByValue.set(nums[index], index);
  }

  return [];
}

Big O notation

MeasureComplexityExplanation
TimeAverage O(n)The array is scanned once, with average constant-time map lookups.
Auxiliary spaceO(n)The map may store an index for every visited value.

How is this practice?

Previous lessonFrequency CountersNext lessonValid Anagram
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation