0

Two pointers: same direction

Patternsused in ~90% of apps

Both markers walk the same way, at different speeds. One reads every item. The other only moves when something is worth keeping. This is how a list gets rewritten in place with no second array.

Core idea. A read pointer visits every position. A write pointer marks where the next keeper goes. The write pointer always trails the read pointer, so you can safely overwrite positions you have already read.
Playground, this isn't a screenshot
removing zeroes from [1, 0, 2, 0, 3]
1
r w
0
2
0
3
keep 1 → write at 0, w moves to 1
1
0
r w
2
0
3
zero → skip, w stays
1
2
w
2
r
0
3
keep 2 → write at 1, w moves to 2
1
2
2
w
0
r
3
zero → skip, w stays
1
2
3
w
0
3
r
keep 3 → write at 2, w moves to 3

w never passes r, so every write lands on a slot already read. The answer is the first 3 positions.

↑ play a little first, then dig in