Remove Duplicates from Sorted Array
In-place deduplication of a sorted array using two pointers.
Problem Statement
Given a sorted integer array nums, remove the duplicates in-place such that each unique element appears only once. Return the number of unique elements k. The first k elements of nums must contain the unique elements in order.
Examples
Example 1
Explanation: Two unique elements. First 2 positions hold [1,2].
Example 2
Constraints
- 1 ≤ nums.length ≤ 3 × 10⁴
- -100 ≤ nums[i] ≤ 100
- nums is sorted in non-decreasing order.
Solutions
Build a new array with only unique elements and copy back. Doesn't satisfy the in-place constraint.
Related Concepts
Deepen your understanding with these related topics from our AI Glossary:
Want to master the core concepts?
Our free AI Glossary covers 190+ topics — from Two Pointers to Dynamic Programming, Machine Learning, SQL, and more. Structured learning tracks for every level.
Stuck? Ask AI to explain it step by step.
Ask Claude, GPT-4o, or Gemini to debug your code, generate test cases, or walk through the intuition. 39+ models. Pay only on days you use it — no subscription required.
Free to start · No credit card required to explore
Get Started Free