← Visualizers
Two Pointers · read/write compaction LeetCode 26

Remove Duplicates from Sorted Array / visualized

The same slow write / fast read machine as LC 27, with one extra idea: what you compare against. Sorting puts equal values side by side, so a value is worth keeping exactly when it differs from the last value you actually keptnums[write − 1], not nums[read-1]. The invariant carries the proof: nums[0, write) is sorted and duplicate-free after every step, so the comparison only ever needs one look back.

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−1] kept prefix duplicate dropped
O(n) time · O(1) extra space  ·  the comparison is against the last kept value, nums[write-1] — the form that survives the jump to LC 80.