4. Remove Duplicates from Sorted Array
easyAsked at WixRemove duplicates from a sorted array in-place; Wix uses two pointers for deduplicating ordered template-component IDs.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given a sorted integer array nums, remove duplicates in-place such that each unique element appears once, and return the new length.
Constraints
1 <= nums.length <= 3 * 10^4nums is sorted non-decreasing
Examples
Example 1
nums=[1,1,2]2 (nums=[1,2,_])Example 2
nums=[0,0,1,1,1,2,2,3,3,4]5Approaches
1. Brute force
Use a Set, copy unique back.
- Time
- O(n)
- Space
- O(n)
const s=[...new Set(nums)]; for(let i=0;i<s.length;i++) nums[i]=s[i]; return s.length;Tradeoff:
2. Two pointers
Write pointer advances only when a new value appears.
- Time
- O(n)
- Space
- O(1)
function removeDuplicates(nums){
if(!nums.length) return 0;
let k=1;
for(let i=1;i<nums.length;i++){
if(nums[i]!==nums[i-1]) nums[k++]=nums[i];
}
return k;
}Tradeoff:
Wix-specific tips
Wix expects you to call out the in-place memory win — a relevant constraint when serializing builder JSON back to their multi-tenant storage.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Remove Duplicates from Sorted Array and other Wix interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →