AlgoMaster Logo

Remove Element

easyFrequency6 min readUpdated June 23, 2026

Understanding the Problem

This looks like a deletion problem, but arrays in most languages can't shrink in place. So the real task is to rearrange the array so that all elements not equal to val sit at the front, then return how many there are. Whatever sits past that prefix is ignored.

One detail drives the choice of algorithm: the problem allows the order of the remaining elements to change. That permission lets us avoid shifting elements we are about to discard. A left-to-right scan with a write pointer works without using that permission, and a two-pointer swap exploits it to do fewer writes.

Key Constraints:

  • 0 <= nums.length <= 100 → The array can be empty, so the code must return 0 without indexing into it.
  • 0 <= nums[i] <= 50 and 0 <= val <= 100 → Since val ranges up to 100 while elements stay within 0 to 50, val may not appear in the array at all. In that case every element is kept and the answer equals the array length.

Approach 1: Extra Array

Intuition

Create a new array, copy over everything that isn't val, then write those kept elements back into the front of nums. This uses extra space rather than editing in place, but it separates the two concerns (selecting elements and placing them) into independent steps, which makes a useful baseline before optimizing.

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 works but allocates a second array. The next approach writes kept elements directly into the front of nums, removing the extra array entirely.

Approach 2: Two Pointers (Front Overwrite)

Intuition

A single write pointer k tracks where the next kept element belongs. A read index i scans the array. Every time nums[i] is not equal to val, we copy it to nums[k] and advance k. Values equal to val are read past without advancing k, so they get overwritten by later kept elements.

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

This approach copies every kept element, even when it already sits in the right place. When val is rare, that is one write per element for almost the whole array. The next approach uses the order-doesn't-matter permission to write only when it finds a val to remove.

Approach 3: Two Pointers (Swap with End)

Intuition

Approach 2 preserves order even though the problem allows reordering, so it writes more often than needed when val is rare. This approach uses two pointers: i from the front and n marking the current end. When nums[i] equals val, we overwrite position i with the element at nums[n-1] and decrement n, discarding the val in one write. After this move, i does not advance, because the element pulled in from the end has not been checked yet and could itself equal val. When nums[i] is not val, we keep it and advance i.

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