Imagine This: Sorting Socks with Your Eyes Closed
Picture this: Youโve just done laundry, and you’re sorting socks. You notice that your sock drawer is already sorted by color. White, white, whiteโฆ blue, blueโฆ red, red.
Now, your task is to remove the duplicates so that there’s only one sock of each color on display.
Sounds simple? Thatโs exactly what weโre going to do with a sorted array!
Whatโs the Problem Statement?
Letโs understand what LeetCode and coding interviews mean by “Remove Duplicates from Sorted Array”

Given:
A sorted list of integers (in non-decreasing order).
Task:
Remove the duplicates in-place. Ensure each unique element appears only once. Return the new length of the modified array.
You must do this without using extra space (i.e., constant space only).
Why Sorted Arrays Help Us Here?
Imagine you’re reading a line of words sorted alphabetically. If one word is the same as the one before, itโs a duplicate, right?
Since the array is already sorted, all duplicates will sit next to each other. This makes it super easy to compare one element with the one before it and decide whether itโs a duplicate.
Real-Life Analogy: Clearing Your Contact List
Think of your phoneโs contact list. You might have multiple entries for “Mom” or “Alex”. If the list is sorted, you can scroll through and remove the extras easily because theyโre all grouped together.
Same goes for our arrayโour job is to scroll through, compare, and keep only the first unique one.
Solution Walkthrough: Step-by-Step
Weโll solve this using the two-pointer technique, which is like having two fingers walking through the array:
Step-by-Step Logic
- One pointer (
i
) keeps track of the last unique element. - The other pointer (
j
) explores the rest of the array. - If
nums[j] != nums[i]
, we movei
forward and copynums[j]
tonums[i]
.
Here’s how it looks:
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Algorithm:
- Start with
i = 0
- For
j
from 1 to len(nums)-1:- If nums[j] != nums[i]:
- Increment i
- nums[i] = nums[j]
- If nums[j] != nums[i]:
Output: New length = 5
Modified array = [0,1,2,3,4,…]
Why This Works Efficiently?
- Time Complexity: O(n) โ we go through the array once.
- Space Complexity: O(1) โ no extra array needed.
Variants You Might See
- What if the array isn’t sorted?
Youโll need to sort it first (O(n log n)) and then apply this logic. - What if we want to return the new array instead of the length?
Just slice:nums[:k]
, wherek
is the new length.
Pro Tips for Mastery
โ
Tip 1: Use the two-pointer technique for any in-place array problems. Itโs a go-to trick! ๐ฃ
โ
Tip 2: Donโt worry about what comes after the new length. Interviewers donโt care โ just modify the first k
elements. ๐
โ
Tip 3: Avoid extra memory unless asked โ space-efficient solutions score more points. ๐
โ
Tip 4: Practice this with strings and characters too โ same principle applies! ๐ค
โ
Tip 5: Naming your pointers meaningfully (unique_index
, scanner
) helps readability. ๐ง
Common Mistakes to Avoid
๐ซ Using another array to store uniques โ not allowed in this problem!
๐ซ Forgetting the array is sorted โ donโt waste time checking all elements.
๐ซ Not updating the array in-place โ donโt return a new one unless asked.
Why This Shows Up in Interviews?
Interviewers love this question because:
- It tests array manipulation.
- It checks your understanding of in-place operations.
- It reveals if you know space/time trade-offs.
Pro Insight: This problem lays the foundation. It helps solve more complex array problems like “Move Zeroes”, “Merge Sorted Arrays”, and “Remove Element”.
Try it Yourself
Final Thoughts
You just crushed one of the most frequently asked array problems.
Give yourself a high five โ or better, run through it again from memory. Youโll be amazed at how much you remember.
Keep Practicing. Keep Crushing It.
See you in the next post, where we break down โ the next logical problem!
FAQs
1. Why is the two-pointer technique ideal for this problem?
Because the array is sorted, duplicates are adjacent. Two pointers let you overwrite duplicates in-placeโsaving space and time.
2. Can I use extra memory or another array?
No, the standard LeetCode/interview problem expects you to do this in-place with O(1) extra space. Using another array is not optimal here.
3. What should I return after removing duplicates?
Return the new length (number of unique elements). The first k elements of the array will be the unique values; what comes after k doesnโt matter.
4. Does this work if the input array isnโt sorted?
No, this solution only works for sorted arrays. For unsorted arrays, sort it first or use a set, but that increases complexity and space usage.
5. Why do interviewers love this problem?
It tests if you can manipulate arrays in-place, think about algorithmic efficiency, and apply classic coding patterns under constraints.