Remove Element

Easy
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Remove Element

Approach

Use a slow pointer to track the write position for kept elements. The fast pointer scans every element. When the fast pointer finds a value that is not the target, copy it to the slow position and advance slow.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Two Pointers (Same Direction)

Why It Works

The slow pointer only advances for non-target elements, effectively overwriting target values and compacting the remaining elements in-place.

Updated Feb 2026