Use a two-pointer technique: one pointer (i) tracks the position of the last unique element, and the other (j) scans the array. When a new unique element is found (nums[j] != nums[i]), increment i and copy nums[j] to nums[i]. Finally, return i+1 as the count of unique elements.
Pro tip: Clarify that the array is sorted and that we only need to return the count, not the modified array itself. Also, mention that the relative order of unique elements is preserved, which is often expected.
Restate the problem: remove duplicates in-place from a sorted array, return the number of unique elements, and use O(1) extra memory. Confirm that the array is sorted and that we can modify it.
Explain that since the array is sorted, duplicates are adjacent. Use two pointers: one to track the last unique element (i) and one to scan the array (j).
Initialize i = 0. Iterate j from 1 to n-1. If nums[j] != nums[i], increment i and set nums[i] = nums[j]. After the loop, return i+1.
Time complexity is O(n) since we traverse the array once. Space complexity is O(1) as we only use two pointers. Handle edge cases: empty array (return 0), single element (return 1), all duplicates (return 1).
Walk through a small example, e.g., [1,1,2] -> length 2, array becomes [1,2,_]. Also test [0,0,1,1,1,2,2,3,3,4] -> length 5, array becomes [0,1,2,3,4,_,_,_,_,_].
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.