2Brute2Best
πŸ”₯0

Remove Duplicates from Sorted Array II

MediumΒ·LeetCode #80 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given an integer array nums sorted in non-decreasing order.

Remove extra copies in place so that every value appears at most twice. The elements you keep must stay in their original order. Return k, the number of elements kept.

After your function runs, the first k slots of nums must hold the kept elements in order. Whatever is after slot k βˆ’ 1 is ignored. Use only O(1) extra memory.

Input

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

Output

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

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

1 appears three times, so one copy is dropped. 2 appears twice and 3 once β€” both are fine.

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

1 appears four times, so two copies are dropped. Every other value already appears at most twice.

Constraints
  • β€’ 1 ≀ nums.length ≀ 3 Β· 10^4
  • β€’ βˆ’10^4 ≀ nums[i] ≀ 10^4
  • β€’ 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 sit together, in the input and in the answer β†’ the last few written elements tell us the count.
β€œin place, keep the order, return k”
β†’ Read / write: build the answer as the prefix `nums[0 … writeβˆ’1]`.
β€œat most twice”
β†’ Look 2 slots back into the written part: keep if `nums[read] != nums[write βˆ’ 2]`.
03

Approach Complexity Ladder

Brute Force β€” count copies with a mapO(N log N)O(N)

Keep an element if the map says fewer than 2 copies are kept; copy back

Better β€” read / write with a run counterO(N)O(1)

Count the length of the current run of equal values; keep the first 2 of each run

Optimal β€” read / write, compare with nums[write βˆ’ 2]O(N)O(1)

Keep nums[read] if write < 2 or nums[read] != nums[write βˆ’ 2]

Brute Force Intuition

Walk nums. Keep a map keptCount[value] of how many copies we have kept. If it is below 2, append the element to a new array and increase the count. Copy the new array back into nums.

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.