NeetCode #184LC-26EasyTwo PointersNC 250
← Back to All Problems#184 · #26 · Remove Duplicates from Sorted Array(删除有序数组中的重复项)
📌 Problem Statement & Constraints
Given an integer array
nums sorted in non-decreasing order, remove the duplicates in place so each element appears once, and return the new length k. The first k slots must hold the unique values in order. Constraints: 1 <= nums.length <= 3 * 10^4, -100 <= nums[i] <= 100.💡 Core Algorithmic Approaches
- Because the array is sorted, duplicates are adjacent, so a single write pointer suffices.
- Keep
kas the number of unique elements written so far. The first element is always kept. - For each subsequent element, write it to
nums[k]only if it differs fromnums[k-1], then incrementk. - The comparison is against the last written element, not the previous read element -- the distinction matters when runs are longer than two.
💻 Benchmark Python3 Implementation
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
k = 1 # first element is always kept
for i in range(1, len(nums)):
if nums[i] != nums[k - 1]: # compare against the last written value
nums[k] = nums[i]
k += 1
return k⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n): one pass.
💾 Space Complexity
O(1): in place.
⚠️ Interview Pitfalls & Follow-ups
- Comparing with
nums[i-1]: this fails when the write pointer lags, becausenums[i-1]may be a value that was already overwritten or skipped. Compare withnums[k-1]instead. - Building a new list: the problem requires in-place modification.
- Initialising
kto 0: the first element is always unique, sokstarts at 1 (and the loop starts at index 1). - Assuming the array is unsorted: sortedness is what makes the adjacent-comparison approach valid.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Two Pointers Problems
中文速览与面试要点
删除有序数组中的重复项
给你一个 非严格递增排列 的数组 nums,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持一致。然后返回 nums 中唯一元素的个数。考虑 nums 的唯一元素的数量为 k,你需要做以下事情确保你的题解可以被通过:更改数组 nums,使 nums 的前 k 个元素包含唯一元素,并按照它们最初在 nums 中出现的顺序排列。nums 的其余元素与 nums 的大小不重要。返回 k。约束:1 ≤ nums.length ≤ 3 × 10^4;-100 ≤ nums[i] ≤ 100;nums 已按非严格递增排列。
核心要点
- 读写双指针(标准解) :k 是「下一个待写入位置」,同时也等于「已确认的唯一元素个数」。遍历数组,若 nums[i] != nums[k−1] 则写入 nums[k] 并令 k += 1 。
- 为什么与 nums[k-1] 比较 : nums[k-1] 是上一个被保留下来的唯一值,与它比较即可判重。由于数组已排序,相同元素必然相邻。
- 原地安全性 :写指针 k 恒不超过读指针 i,因此不会覆盖尚未读取的元素。
- 复杂度 :单次遍历,O(n) 时间、O(1) 空间。
常见陷阱
- 与 nums[i-1] 比较 :对于「每个元素只出现一次」的题目,比较对象可以是 nums[i-1] (因为去重后相邻元素必然不同);但推广到「最多保留两次」时就必须与 nums[k-1] 或 nums[k-2] 比较。统一用 nums[k-1] 更通用。
- 忘记 k 的初值为 1 :数组非空时第一个元素必然保留。若初值设为 0,后续比较 nums[k-1] 会越界。
- 返回 len(set(nums)) :这只返回长度,但没有 原地修改 nums 的前 k 项,判题会失败。
- 用 del nums[i] 删除 :list 的删除是 O(n),整体退化为 O(n²)。
参考实现
class Solution:
def removeDuplicates(self, nums: list[int]) -> int:
if not nums:
return 0
k = 1 # 第一个元素必然保留
for i in range(1, len(nums)):
if nums[i] != nums[k - 1]: # 与上一个保留值比较
nums[k] = nums[i]
k += 1
return k