Use a two-pointer technique: one pointer (write) tracks the position for the next unique element, and the other (read) scans the array. Since the array is sorted, duplicates are adjacent, so compare the current element with the last written unique element and write only when different. Return the write pointer as the count of unique elements.
Pro tip: Clarify upfront that 'remove duplicates in-place' means modifying the input array and returning the new length, not creating a new array. Mention that the first element is always unique, so start the write pointer at index 1 and handle edge cases like empty or single-element arrays.
Confirm that in-place modification is required, O(1) extra space, and that the return value is the count of unique elements. Discuss edge cases: empty array, single element, all duplicates.
Set write pointer to 1 (since first element is always unique) and read pointer to 1. If array length is 0, return 0 immediately.
For each read pointer from 1 to n-1, compare nums[read] with nums[write-1]. If different, assign nums[write] = nums[read] and increment write.
After the loop, write pointer equals the number of unique elements. Return write.
State that time complexity is O(n) because each element is visited once, and space complexity is O(1) since only two pointers are used.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.