2Brute2Best
πŸ”₯0

Remove Duplicates from Sorted Array

EasyΒ·LeetCode #26 Pro OnlyBasic Reinforcement
LeetCode
01

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.

Input

An integer array nums, sorted in non-decreasing order.

Output

An integer k. The judge also checks that nums[0 … kβˆ’1] is exactly the unique values, in order.

Example 1
Input: nums = [1, 1, 2]
Output: 2, nums = [1, 2, _]

The unique values are 1 and 2. They go in the first 2 slots.

Example 2
Input: nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
Output: 5, nums = [0, 1, 2, 3, 4, _, _, _, _, _]

Each of 0, 1, 2, 3, 4 is kept once, in order.

Constraints
  • β€’ 1 ≀ nums.length ≀ 3 Β· 10^4
  • β€’ βˆ’100 ≀ nums[i] ≀ 100
  • β€’ nums is sorted in non-decreasing order
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œsorted in non-decreasing order”
β†’ Copies of a value are next to each other β†’ one comparison catches a repeat.
β€œremove … in place, keep the order”
β†’ Build the answer at the front of `nums` with a `write` pointer.
β€œreturn k, first k elements are checked”
β†’ The answer is the prefix `nums[0 … writeβˆ’1]`.
03

Approach Complexity Ladder

Brute Force β€” put everything in a setO(N log N)O(N)

Insert all values into a set, then copy the set back

Optimal β€” read / write, compare with the last kept valueO(N)O(1)

Keep nums[read] only if it differs from nums[write βˆ’ 1]

Brute Force Intuition

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:

One pointer reads every element, the other marks where the next "kept" element should be written.
Remember this Β· Two Pointers
Trigger
Sorted array, pairs/triplets with a target, comparing from both ends, compacting an array in place.
Think
If I compare the elements at two positions, can I safely discard one side?
Key question
Which pointer moves, and why can the skipped elements never be the answer?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.