Keep one copy of each value in a sorted array, in place, and return how many are left. From shifting elements left (O(n²)) and building a new list (O(n) memory), to a read pointer and a write pointer in one pass. Then at most k copies, unsorted input, and de-duplicating sorted ids in a data pipeline.
about 10 min 3 Python examples 2 quick checks
Problem.nums is sorted in non-decreasing order. Remove duplicates in place so each value appears once,
keeping the order, and return k, the number of unique values. The first k slots must hold them; what's after
doesn't matter.
ML context. Sorted id lists are everywhere: user ids before a join, document ids from a retrieval shard, sorted
event keys in a log. De-duplicating them in place, in one pass and without a hash set, is how you keep a join from
double-counting rows, and it's exactly what a "unique" step does on sorted data.
1. Step 1: Clarify
That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson
Unlock "Remove Duplicates from Sorted Array"
The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.
Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied