61. Remove Duplicates from Sorted Array II
mediumAsked at PlaidRemove duplicates in-place such that each element appears at most twice. Plaid asks this as a two-pointer variant — exactly the shape they use to enforce 'at most 2 retries per transaction in the same minute' logs.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Plaid loops.
- LeetCode Discuss (2026)— Plaid OA two-pointer.
- Glassdoor (2025)— Plaid SWE II screen.
Problem
Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same. Return k after placing the final result in the first k slots of nums.
Constraints
1 <= nums.length <= 3 * 10^4-10^4 <= nums[i] <= 10^4nums is sorted in non-decreasing order.
Examples
Example 1
nums = [1,1,1,2,2,3]5, nums = [1,1,2,2,3,_]Example 2
nums = [0,0,1,1,1,1,2,3,3]7, nums = [0,0,1,1,2,3,3,_,_]Approaches
1. Count occurrences with Map
Count occurrences, then rebuild with at most 2 of each.
- Time
- O(n)
- Space
- O(n)
// Works but uses extra space and ignores sortedness.Tradeoff: Doesn't exploit the sorted input.
2. Two-pointer with k-tolerance
Generalizable to 'at most k duplicates'. Write pointer trails. If nums[read] != nums[write - k], write.
- Time
- O(n)
- Space
- O(1)
function removeDuplicates(nums) {
const k = 2;
let write = 0;
for (const n of nums) {
if (write < k || nums[write - k] !== n) {
nums[write++] = n;
}
}
return write;
}Tradeoff: Generalizes to any k. The check 'nums[write - k] !== n' verifies the candidate is different from the k-th-most-recently-written, which guarantees at most k of any value.
Plaid-specific tips
Plaid loves the k-generalized version because it shows you've abstracted beyond k=2. Bonus signal: explain why nums[write - k] is the right comparison — it's the value that would be the (k+1)th copy if we wrote n. Connect to rate-limiting logs where each user can have at most k retries per minute.
Common mistakes
- Hard-coding k=2 with a counter — fine but doesn't show generalization.
- Comparing nums[read] to nums[read-1] — doesn't capture 'at most 2 in window'.
- Off-by-one on write index when k > write.
Follow-up questions
An interviewer at Plaid may pivot to one of these next:
- At most k duplicates (parameter).
- Same problem on a linked list (LC 82).
- Stream version with bounded memory.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why nums[write - k] and not nums[write - 1]?
Comparing to write-1 only catches the immediate predecessor; we'd allow [1,1,1,1] because each new 1 differs from no predecessor. write-k catches the k-th previous.
What if the array has < k elements?
The 'write < k' check writes everything without comparison.
Practice these live with InterviewChamp.AI
Drill Remove Duplicates from Sorted Array II and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →