4. Remove Duplicates from Sorted Array
easyAsked at PlaidIn-place de-duplicate a sorted array and return the new length. Plaid asks this because de-duping near-duplicate transactions (same id, same amount, slight timestamp drift) is a daily reality on their ingestion pipeline.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Plaid loops.
- Glassdoor (2025)— Plaid SWE I onsite warm-up.
- LeetCode Discuss (2026)— Frames as dedup of bank-feed entries.
Problem
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Return k, the number of unique elements.
Constraints
1 <= nums.length <= 3 * 10^4-100 <= nums[i] <= 100nums is sorted in non-decreasing order.
Examples
Example 1
nums = [1,1,2]2, nums = [1,2,_]Example 2
nums = [0,0,1,1,1,2,2,3,3,4]5, nums = [0,1,2,3,4,_,_,_,_,_]Approaches
1. Use a Set then copy back
Push everything into a Set, sort, write back.
- Time
- O(n log n)
- Space
- O(n)
function removeDuplicates(nums) {
const u = [...new Set(nums)].sort((a,b)=>a-b);
for (let i = 0; i < u.length; i++) nums[i] = u[i];
return u.length;
}Tradeoff: Allocates a Set and an array. Wasteful given the input is already sorted.
2. Two-pointer in-place write
A write pointer trails a read pointer. Advance the writer only when the current value differs from the previous unique.
- Time
- O(n)
- Space
- O(1)
function removeDuplicates(nums) {
if (nums.length === 0) return 0;
let write = 1;
for (let read = 1; read < nums.length; read++) {
if (nums[read] !== nums[read - 1]) {
nums[write++] = nums[read];
}
}
return write;
}Tradeoff: Linear time, constant space. The 'compare to nums[read-1]' trick is what saves you from tracking last-seen as a separate variable.
Plaid-specific tips
Plaid grades this on whether you exploit the sortedness you were given. State the precondition explicitly before coding. Bonus signal: explain how the two-pointer pattern is exactly what they use when de-duping consecutive webhook deliveries with the same idempotency key.
Common mistakes
- Comparing nums[read] to nums[write] (off-by-one) instead of nums[read-1].
- Returning the array instead of k — re-read the prompt.
- Allocating a new array — wastes the in-place requirement.
Follow-up questions
An interviewer at Plaid may pivot to one of these next:
- Allow each unique element at most twice (LC 80).
- Same problem on a sorted linked list (LC 83).
- What if the array is too large to fit in memory? Stream + writer pointer over a chunked file.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why compare to the previous element instead of tracking 'last unique'?
Because the array is already sorted, the previous-position approach implicitly tracks last-unique with zero extra state.
Does this work if nums is unsorted?
No — the algorithm relies on duplicates being adjacent. For unsorted input use a hash set, which costs O(n) space.
Practice these live with InterviewChamp.AI
Drill Remove Duplicates from Sorted Array 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 →