Remove Duplicates from Sorted Array - Problem

You're given a sorted array of integers that may contain duplicates. Your task is to remove duplicates in-place so that each unique element appears only once, while maintaining the original sorted order.

The challenge is to do this without using extra space for another array. Instead, you need to modify the original array and return the number of unique elements.

Important: You don't need to worry about elements beyond the first k positions (where k is the number of unique elements). The judge will only check the first k elements of your modified array.

Example: If you have [1,1,2,2,3], you should modify it to [1,2,3,_,_] and return 3. The underscores represent elements that can be ignored.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,1,2]
โ€บ Output: 2, nums = [1,2,_]
๐Ÿ’ก Note: The function returns 2 since there are two unique elements: 1 and 2. The array is modified to [1,2,_] where _ represents elements that can be ignored.
example_2.py โ€” Multiple Duplicates
$ Input: nums = [0,0,1,1,1,2,2,3,3,4]
โ€บ Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
๐Ÿ’ก Note: There are five unique elements: 0, 1, 2, 3, and 4. After removing duplicates in-place, the first 5 positions contain these unique elements in sorted order.
example_3.py โ€” Edge Case
$ Input: nums = [1]
โ€บ Output: 1, nums = [1]
๐Ÿ’ก Note: Single element array has only one unique element, so the array remains unchanged and function returns 1.

Visualization

Tap to expand
Library Shelf - Before OrganizationBook ABook ABook BBook BBook CSLOW-1FASTSLOWAfter Processing - Organized ShelfBook ABook BBook CEmptyEmptyKeptKeptKeptIgnoredIgnoredKey Algorithm Steps1. Start with slow=1 (first book always unique)2. Compare current book with last unique book placed3. If different: place at slow position, increment slow4. Return slow (count of unique books)
Understanding the Visualization
1
Setup Bookmarks
Place 'slow' bookmark at position 1 (first book is always kept). 'Fast' bookmark scans all books.
2
Compare Titles
Compare book at fast bookmark with the last unique book placed (at slow-1).
3
Place Unique Books
If titles are different, move the book to slow position and advance slow bookmark.
4
Continue Process
Keep scanning with fast bookmark until all books are checked.
Key Takeaway
๐ŸŽฏ Key Insight: Since the array is sorted, duplicates are always adjacent! We only need to compare each element with the previous unique element we've placed, making this an O(n) single-pass solution.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array with two pointers

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using two pointer variables, no extra data structures

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 3 ร— 104
  • -100 โ‰ค nums[i] โ‰ค 100
  • nums is sorted in non-decreasing order
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
95.2K Views
Very High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen