AlgoMaster Logo

Remove Element

Last Updated: March 31, 2026

easy

Understanding the Problem

At first, this seems like you just need to delete elements from an array. But arrays in most languages don't support true deletion, you can't shrink them. So the real task is to rearrange the array so that all elements not equal to val are packed at the front, and then return how many there are.

The key detail: the problem says the order of remaining elements may be changed. This is more relaxed than Move Zeroes (which requires preserving relative order), and that relaxation opens up an interesting optimization. But even if we ignore it, a simple left-to-right scan with a write pointer works perfectly.

Key Constraints:

  • 0 <= nums.length <= 100 → With n at most 100, even O(n^2) would be trivially fast. This is an Easy problem where the focus is on technique, not performance.
  • 0 <= nums[i] <= 50 → All values are non-negative and small. No tricky edge cases with negative numbers or large values.
  • 0 <= val <= 100 → The value to remove might not even exist in the array (val could be 51-100 while all elements are 0-50).

Approach 1: Extra Array

Intuition

The simplest way to think about this: create a new array, copy over everything that isn't val, then copy it all back. This violates the spirit of "in-place," but it makes the logic dead simple and is a good starting point for understanding the problem.

Algorithm

  1. Create a new array result.
  2. Iterate through nums. For each element not equal to val, add it to result.
  3. Copy the elements from result back into the first positions of nums.
  4. Return the length of result.

Example Walkthrough

Input:

0
3
1
2
2
2
3
3
nums

After the first pass (collect non-val elements into result):

0
2
1
2
result

Finally, we copy result back into nums:

0
2
1
2
2
2
3
3
nums

Code

This approach works correctly but uses extra space.

What if we wrote non-val elements directly into their final positions within the original array?

Approach 2: Two Pointers (Front Overwrite)

Intuition

Here's the key insight: we can use a single pointer k to track where the next "kept" element should be placed. As we scan through the array with index i, every time nums[i] is not equal to val, we write it at position k and advance the pointer.

Algorithm

  1. Initialize k = 0 to track the write position.
  2. Iterate through the array with index i from 0 to n - 1.
  3. When nums[i] != val, write nums[i] to nums[k] and increment k.
  4. After the loop, k is the count of non-val elements, and nums[0..k-1] contains them all.

Example Walkthrough

1Initialize: k=0, i=0
0
k
0
i
1
1
2
2
3
2
4
3
5
0
6
4
7
2
1/6

Code

The front overwrite approach copies every kept element, even when it's already in position. Since the problem says order doesn't matter, can we do fewer writes by swapping with the end instead?

Approach 3: Two Pointers (Swap with End)

Intuition

The problem says the order of remaining elements doesn't matter. Approach 2 preserves order anyway, which means it does more work than necessary when val is rare. The trick: use two pointers, one at the start and one at the end. When nums[i] equals val, swap it with the last element and shrink the effective array.

Algorithm

  1. Initialize i = 0 and n = nums.length (n tracks the effective end of the array).
  2. While i < n:
    • If nums[i] == val, copy nums[n - 1] to nums[i] and decrement n. Don't advance i because the swapped-in element hasn't been checked yet.
    • If nums[i] != val, advance i.
  3. Return n.

Example Walkthrough:

1Initialize: i=0, n=4
0
3
i
1
2
2
2
3
n-1
3
1/6

Code