4. Remove Duplicates from Sorted Array
easyAsked at VercelRemove duplicates from a sorted array in-place and return the new length. Vercel asks this to test whether you can shed allocations — relevant when their edge runtime has a tight memory budget per function invocation.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-12)— Vercel edge runtime team screen; in-place mutation expected.
- Reddit r/cscareerquestions (2026-Q1)— Mentioned in Vercel new-grad screen with explicit 'no extra memory' constraint.
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 the number of unique elements k. The first k elements of nums should hold the final result.
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,_]Explanation: First 2 elements are unique.
Example 2
nums = [0,0,1,1,1,2,2,3,3,4]5, nums = [0,1,2,3,4,_,_,_,_,_]Approaches
1. Copy to Set then back
Build a Set, convert to array, overwrite original.
- Time
- O(n)
- Space
- O(n)
function removeDuplicates(nums) {
const unique = [...new Set(nums)];
for (let i = 0; i < unique.length; i++) nums[i] = unique[i];
return unique.length;
}Tradeoff: Violates the in-place constraint. Mention it but pivot to the two-pointer.
2. Two-pointer in-place (optimal)
Slow pointer marks the position to write; fast pointer scans. When nums[fast] differs from nums[slow], advance slow and copy.
- Time
- O(n)
- Space
- O(1)
function removeDuplicates(nums) {
if (nums.length === 0) return 0;
let slow = 0;
for (let fast = 1; fast < nums.length; fast++) {
if (nums[fast] !== nums[slow]) {
slow++;
nums[slow] = nums[fast];
}
}
return slow + 1;
}Tradeoff: O(1) extra space because we mutate in place. The slow/fast pattern is the canonical answer for any in-place dedup or filter on a sorted sequence.
Vercel-specific tips
Vercel cares that you stay in-place — they will explicitly say 'no extra memory' and you should code the two-pointer, not the Set. Bonus signal: handling the empty array, and noticing that the returned k matters more than the array tail (the tail is undefined-behavior).
Common mistakes
- Returning nums.length instead of slow + 1 — passes nothing because the array still has the old length.
- Using `nums.slice` or `nums.filter` — allocates a new array, violating in-place.
- Forgetting the empty-array guard — slow starts at 0 and is returned as 1.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Allow each element to appear at most twice (LC 80).
- Remove all instances of a target value in-place (LC 27).
- What if the array isn't sorted? Hash-set in-place is harder; you'd usually allocate.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why two pointers and not one?
One pointer can only iterate; you need a second cursor to know where to WRITE the next unique value. Slow = write head, fast = read head — same shape as the in-place filter pattern.
Do I need to clear the tail of the array?
No. LeetCode's grader only checks the first k slots. In production code you'd `nums.length = slow + 1` to truncate, but for the interview, return k.
Practice these live with InterviewChamp.AI
Drill Remove Duplicates from Sorted Array and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →