Step 3: Solve Problems on Arrays [Easy -> Medium -> Hard]›Easy
Remove Duplicates & The Clean Bookshelf
EasyFunction: removeDuplicatesFromSortedArray()
ASCI Mission Breakdown • Simple as Hell
"Compress sorted array in-place so unique elements sit in front, returning unique count."
Real-World Metaphor:
Imagine stacking plates. You have duplicate designs. You slide one copy of each unique design to the neat front stack, leaving extras behind. At the end, you count how many unique designs you stacked.
Interactive Visual WalkthroughARRAY-POINTERS
Step 1 / 3
Sorted Shelf: [1, 1, 2, 2, 3]
i
1
[0]
j
1
[1]
2
[2]
2
[3]
3
[4]
Memory Notepad / State Tracker
Unique Count:1
Evaluating
1. Initialize Pointers
i = 0 holds value 1. j scans index 1 (also 1). Duplicate! j skips forward.
How to Think About This (Mental Model)
Use a slow pointer i = 0 to mark the last known unique item position.
Use a fast pointer j = 1 to scan ahead through the array.
Whenever nums[j] !== nums[i], we discovered a brand new number! Increment i and copy nums[i] = nums[j].
At the end, return i + 1 (the count of unique items).
### The Mission
Imagine a library bookshelf with books arranged in alphabetical order, but there are multiple redundant copies of each book.
Your mission is to rearrange the shelf **in-place** so that all unique books are slid to the front of the shelf, and return the total count of unique books **k**. The leftover slots at the end do not matter.
Examples
Example 1
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints
1 <= nums.length <= 5 * 10^4
-5 * 10^4 <= nums[i] <= 5 * 10^4
Topic Tags:
Solve Problems on Arrays [Easy -> Medium -> Hard]EasyremoveDuplicatesFromSortedArray