0
Challenge · 24Array & String Patterns
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.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
removing zeroes from [1, 0, 2, 0, 3]
1
r w
0
2
0
3
1
0
r w
2
0
3
1
2
w
2
r
0
3
1
2
2
w
0
r
3
1
2
3
w
0
3
r
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