Remove Element

easy

Remove all instances of value in-place, return new length

Remove Element

Key Insight

Slow pointer builds result, fast scans. Copy non-target values to slow position.

Step 1Setup
Remove value: 3
slow
fast
3
0
2
1
2
2
3
3
same direction

Remove all 3s from [3,2,2,3]. slow=0, fast scans.

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Remove Element

Slow pointer builds result, fast scans. Copy non-target values to slow position.

  1. Setup

    Remove all 3s from [3,2,2,3]. slow=0, fast scans.

  2. Skip Target

    fast=0 is 3 (target). Skip it.

  3. Keep Non-Target

    fast=1 is 2. Copy to slow position.

  4. Keep Another

    fast=2 is 2. Copy to slow=1.

  5. Done

    New length = 2. First 2 elements are valid.