Skip to content

visual walkthrough

Remove Duplicates from Sorted Array

EasySame Direction (Read/Write)Reported at: MetaAmazonMicrosoft+6

Introduces the reader/writer pointer pair.

Solve on LeetCode

The idea

Because the array is sorted, duplicates sit next to each other. A `read` pointer scans every value while a `write` pointer marks the end of the unique prefix.

When the value at `read` differs from the last kept value, copy it forward. Everything left of `write` is the answer.

Complexity

approachtimespace
Copy to a new listO(n)O(n)
Read / write pointersO(n)O(1)

More walkthroughs