4. Remove Duplicates from Sorted Array
easyAsked at RedditRemove duplicates in-place from a sorted array and return the new length. Reddit uses this to test two-pointer fluency — the same pattern they use to dedupe vote events from the same user_id arriving across replicas.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit phone-screen warm-up for backend roles.
- LeetCode Discuss (2025-10)— Frequently the 'first 10 minutes' question before fraud-detection follow-ups.
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 in nums.
Constraints
1 <= nums.length <= 3 * 10^4-100 <= nums[i] <= 100nums is sorted in non-decreasing order.
Examples
Example 1
nums = [1,1,2]2Explanation: nums becomes [1,2,_] and we return 2.
Example 2
nums = [0,0,1,1,1,2,2,3,3,4]5Explanation: nums becomes [0,1,2,3,4,_,_,_,_,_].
Approaches
1. Set-based copy
Build a Set, copy unique values back, return size.
- Time
- O(n)
- Space
- O(n)
function removeDuplicates(nums) {
const set = new Set(nums);
const unique = [...set];
for (let i = 0; i < unique.length; i++) nums[i] = unique[i];
return unique.length;
}Tradeoff: Linear time but O(n) extra memory — fails the in-place spirit of the question.
2. Two-pointer (slow/fast) in-place (optimal)
Slow pointer marks the next unique slot. Fast pointer scans. When nums[fast] != nums[slow], advance slow and write.
- 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: Linear time, O(1) space, in-place. Same template Reddit uses for compacting vote-event log segments after dedup.
Reddit-specific tips
Reddit interviewers care that you talk about the invariant: 'everything left of slow is unique and sorted; everything right of slow is unread.' Bonus signal: relate it to log-compaction in their Cassandra-derived vote event store.
Common mistakes
- Returning slow instead of slow + 1 (off by one).
- Starting slow at 1 (the first element is always unique already).
- Forgetting that the relative order constraint forbids swapping.
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Remove duplicates allowing 2 occurrences (LC 80).
- Remove element (LC 27).
- What if the input isn't sorted? (Need hash set, lose O(1) space.)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does it work without comparing nums[fast] to nums[fast-1]?
Since the array is sorted, nums[slow] is always the last unique value we've written. Comparing to slow is equivalent and simpler.
Does the question want me to return the modified array or the length?
Just the length — the grader inspects nums[0..k-1] separately. Don't slice/return a new array.
Practice these live with InterviewChamp.AI
Drill Remove Duplicates from Sorted Array and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →