← Visualizers
Two Pointers · read/write compaction LeetCode 80

Remove Duplicates from Sorted Array II / visualized

At most two copies of each value survive. The generalisation is one character wide: keep nums[read] when write < 2 || nums[read] != nums[write-2]. If the slot two behind the write head already holds this value, two copies are in the answer and this is the third. Swap the 2 for k and the same template solves “at most k duplicates”. The trap: looking back at nums[read-2] instead. read walks the original array while write walks the answer you are building, and the moment they separate those are two different arrays.

Execution

idle
[0, write) — kept write — next slot stale / unread
Press Run to begin.
0 / 0
Speed

Java · running line

write (slow) read (fast) nums[write−2] lookback kept prefix third copy dropped
O(n) time · O(1) extra space  ·  write < 2 || nums[read] != nums[write-2] — replace the 2 with k for “at most k duplicates”.