Skip to main content
Understanding the Two Pointers (a.k.a. Multiple Pointers) Pattern in JavaScript
  • JavaScript
  • Algorithms
  • Data Structures
  • Performance Optimization
  • Problem Solving

Understanding the Two Pointers (a.k.a. Multiple Pointers) Pattern in JavaScript

Learn how to use the Two Pointers pattern in JavaScript to optimize your algorithms from O(N²) to O(N) time complexity. Perfect for solving problems with sorted arrays and strings efficiently.

3 min readSoulaimane Boukrab

Two Pointers VisualizationTwo Pointers Visualization

When solving problems with arrays or strings, sometimes one loop just isn't enough. That's where the Two Pointers pattern comes in.

It's a simple but powerful technique you'll see in coding interviews, problem-solving, and even real-world projects.

What is the Two Pointers Pattern?

The idea is straightforward: 👉 Use two variables (pointers) that represent positions in a collection (like an array). 👉 Move those pointers towards each other or in the same direction, depending on the problem.

This lets us avoid nested loops and gives us much faster solutions.

Think of it like two people searching for each other in a crowd. Instead of one person calling out to everyone, both move strategically until they meet. Much faster!

Example Problem: Find the First Pair That Sums to Zero

We'll write a function called sumZero that accepts a sorted array of integers. The goal: return the first pair that adds up to zero.

Examples:

javascript
sumZero([-4, -3, -2, -1, 0, 1, 2, 5]); // [-2, 2]
sumZero([-2, 0, 1, 3]); // undefined

Notice: the array must be sorted for this approach to work.

The Naive Solution

The most basic way is using two nested loops:

javascript
function sumZero(arr) {
  for (let i = 0; i < arr.length; i++) {
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[i] + arr[j] === 0) {
        return [arr[i], arr[j]];
      }
    }
  }
}

This works… but it has O(n²) time complexity. With a large array (say 10,000 elements), that's extremely inefficient.

The Refactored Solution (Using Two Pointers)

Two Pointers Start and EndTwo Pointers Start and End

Because the array is sorted, we can do better:

Step 1: Initialize the pointers

  • Place one pointer (left) at the start of the array
  • Place another pointer (right) at the end of the array

Step 2: Check and adjust While the pointers haven't met, check the sum of both numbers:

  • If the sum equals 0 → We've found the pair! Return it.
  • If the sum is too big → Move the right pointer left (decrease right)
  • If the sum is too small → Move the left pointer right (increase left)
javascript
function sumZero(arr) {
  let left = 0;
  let right = arr.length - 1;

  while (left < right) {
    let sum = arr[left] + arr[right];

    if (sum === 0) {
      return [arr[left], arr[right]];
    } else if (sum > 0) {
      right--;
    } else {
      left++;
    }
  }
}

Example run:

javascript
sumZero([-4, -3, -2, -1, 0, 1, 2, 10]); // [-2, 2]

This runs in O(n) time — a huge improvement compared to the naive solution.

Why This Works

Because the array is sorted:

  • Adding the smallest and largest values gives us a clear signal about whether to move left or right.
  • By shifting the correct pointer, we eliminate large chunks of unnecessary checks.

Takeaway

The Two Pointers pattern is a powerful tool whenever you're:

  • Working with a sorted array or string
  • Looking for pairs or relationships between values

Instead of brute force with nested loops, smart pointers make your code cleaner and much more efficient.