21
How would you implement a function to remove duplicates from a sorted array in place?
Tap to write answer
0 words | 0 charsPress Enter ↵ to reveal
Your Attempt
0 wordsRefined Model Answer
ReferenceI would use two pointers, one for reading and one for writing. Since the array is already sorted, duplicates appear next to each other, so I can compare the current element with the last unique element and copy it forward only when it is different. The reason I choose this approach is that it gives O(n) time and O(1) extra space. I would also mention whether the function should return the new length or the modified array, because that changes the interface. Edge cases include empty arrays and arrays with all identical values.