4. Remove Duplicates from Sorted Array
easyAsked at SnowflakeIn-place deduplicate a sorted array, returning the new length. Snowflake asks this to test two-pointer mechanics and to set up a follow-up on dictionary-encoded columnar deduplication.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2026-Q1)— Snowflake intern phone screens use this as a two-pointer warm-up.
- LeetCode Discuss (2025-08)— Recurring at Snowflake SDE-I screens.
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. Then 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]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. Set + rebuild
Insert into a Set, then write back. Violates the in-place requirement.
- Time
- O(n)
- Space
- O(n)
function removeDuplicates(nums) {
const set = new Set(nums);
const arr = [...set].sort((a,b) => a - b);
for (let i = 0; i < arr.length; i++) nums[i] = arr[i];
return arr.length;
}Tradeoff: Violates the O(1) extra space requirement. Mention only to show you read the constraint.
2. Two-pointer in-place (optimal)
Slow pointer writes unique values; fast pointer scans. When nums[fast] != 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: Linear, O(1) extra space — and it's exactly the loop that builds a run-length-encoded column.
Snowflake-specific tips
Snowflake interviewers want to hear you connect this to dictionary encoding and run-length encoding in columnar storage. Bonus signal: extend to 'count duplicates per value' and discuss how RLE saves bytes for sorted low-cardinality columns.
Common mistakes
- Returning the array instead of the length (the problem asks for length and mutates in-place).
- Starting slow at 1 instead of 0 — off-by-one in the return value.
- Comparing nums[fast] to nums[fast-1] instead of nums[slow] — works for sorted input but breaks if you generalize to nearly-sorted.
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Allow each value to appear at most twice (LC 80).
- Remove duplicates from unsorted array preserving order.
- Build a run-length-encoded representation in-place.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why O(1) space matters here?
In columnar storage, you cannot afford to allocate a second array the size of a column. In-place compaction is the realistic constraint.
Why does sorted input matter?
Sorted input means duplicates are adjacent. That's also why Snowflake's micro-partitions sort on clustering keys — it makes downstream dedup, RLE, and predicate pushdown cheap.
Practice these live with InterviewChamp.AI
Drill Remove Duplicates from Sorted Array and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →