Remove Duplicates In Place
Use a slow and fast pointer pair.
Remove Duplicates In Place is a free Coding Interview Prep lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Drop Duplicates In Place
Given a sorted array, keep one copy of each value using no extra array. Doing it in place saves memory and is a classic interview ask. 🧹
Why Sorted Helps
When the array is sorted, every duplicate sits right next to its twin. So you only ever compare neighbors, never the whole array.
Two Roles, Two Pointers
Use a slow pointer that marks the last kept value and a fast pointer that scans ahead looking for something new.
slow = 0
fast = 1The Slow Pointer Writes
Think of slow as the write position: everything at or before it is already cleaned and unique.
The Fast Pointer Reads
The fast pointer just reads forward. It races ahead and only signals slow when it spots a value not yet kept.
Skip the Repeats
If a[fast] equals a[slow], it is a repeat, so do nothing but advance fast. The duplicate gets quietly passed over.
for fast in range(1, n):
if a[fast] == a[slow]:
continueFound Something New
When a[fast] differs, move slow forward and copy the new value there. This overwrites old duplicates with fresh unique data.
else:
slow += 1
a[slow] = a[fast]The Answer Is the Length
After the scan, slow + 1 is the count of unique values, all packed at the front of the array.
return slow + 1Ignore the Tail
Whatever sits after the unique prefix is leftover junk. The problem only cares about the first slow + 1 elements, so leave the tail alone.
Mind the Empty Array
An empty array has zero uniques. Guard for n == 0 before starting so you do not read past the end.
if n == 0:
return 0One Pass, No Extra Space
This slow-fast pattern runs in O(n) time and O(1) extra space, which is exactly what tight memory limits demand.
Quick Check
You are removing duplicates in place on a sorted array with slow and fast pointers.
Recap
On a sorted array, a slow-fast pair removes duplicates in one O(n) pass with no extra space, returning slow + 1 as the unique count. 🎉
Frequently asked questions
Is the “Remove Duplicates In Place” lesson free?
Yes — the full text of “Remove Duplicates In Place” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “Remove Duplicates In Place”?
Use a slow and fast pointer pair. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Coding Interview Prep?
No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Remove Duplicates In Place” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Coding Interview Prep lesson?
Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Two Pointers on a Sorted Array
- Find a Pair with a Given Sum
- Remove Duplicates In Place
- Merge Two Sorted Sequences