Remove Duplicates from Sorted Array
Problem Statement
You are given an integer array nums sorted in non-decreasing order (it may contain repeated values).
Remove the repeats in place so that each value appears only once, and keep the values in their original order. Return k, the number of unique values.
After your function runs, the first k slots of nums must hold the unique values in increasing order. Whatever is left after slot k β 1 is ignored.
An integer array nums, sorted in non-decreasing order.
An integer k. The judge also checks that nums[0 β¦ kβ1] is exactly the unique values, in order.
The unique values are 1 and 2. They go in the first 2 slots.
Each of 0, 1, 2, 3, 4 is kept once, in order.
- β’ 1 β€ nums.length β€ 3 Β· 10^4
- β’ β100 β€ nums[i] β€ 100
- β’ nums is sorted in non-decreasing order
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Insert all values into a set, then copy the set back
Keep nums[read] only if it differs from nums[write β 1]
A set keeps only one copy of each value and stores them sorted. Insert every element, then copy the set back into the front of nums and return its size.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.
Circular Buffer & Ring Queue
Two pointer read/write head synchronization in production memory pools.
Pointers vs References Deep Dive
Pointer arithmetic, dereferencing semantics, and memory layout behind in-place array passes.
const Correctness & East-Const
Passing read-only spans with const int* without copying buffers.