4. Remove Duplicates from Sorted Array
easyAsked at ServiceNowStrip duplicates from a sorted array in place, returning the new length. ServiceNow uses this to test the slow/fast two-pointer pattern they apply when collapsing repeated ticket events into a single change record.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in ServiceNow loops.
- Glassdoor (2026-Q1)— Listed in ServiceNow new-grad warmups alongside Two Sum.
- LeetCode Discuss (2025-08)— Posted as an in-place mutation problem on a ServiceNow loop.
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 must hold the unique values.
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. Allocate new array
Walk nums, push to a new array only if it differs from the last pushed.
- Time
- O(n)
- Space
- O(n)
function removeDuplicates(nums) {
const out = [];
for (const v of nums) if (out[out.length-1] !== v) out.push(v);
for (let i = 0; i < out.length; i++) nums[i] = out[i];
return out.length;
}Tradeoff: Correct but violates the in-place requirement. ServiceNow flags this as missing the point of the problem.
2. Slow/fast two-pointer in place
Keep a slow pointer at the next write-slot. Walk fast pointer; when nums[fast] != nums[slow-1], write nums[fast] to nums[slow] and advance slow.
- Time
- O(n)
- Space
- O(1)
function removeDuplicates(nums) {
if (nums.length === 0) return 0;
let slow = 1;
for (let fast = 1; fast < nums.length; fast++) {
if (nums[fast] !== nums[fast - 1]) {
nums[slow] = nums[fast];
slow++;
}
}
return slow;
}Tradeoff: Linear time, constant extra space. The slow/fast pattern generalizes to remove-elem and remove-k-duplicates.
ServiceNow-specific tips
ServiceNow grades for the in-place mutation insight — they explicitly read the slow/fast pattern as a proxy for whether you can compact event streams without doubling memory. Bonus signal: state the invariant 'nums[0..slow) is the deduped prefix' out loud before writing the loop.
Common mistakes
- Forgetting the early-return on empty array (nums.length === 0).
- Initializing slow = 0 instead of slow = 1 — overwrites the first element with itself but breaks the invariant.
- Comparing nums[fast] to nums[slow] instead of nums[fast-1] — works on this sorted input but breaks if you generalize.
Follow-up questions
An interviewer at ServiceNow may pivot to one of these next:
- Allow each element to appear at most twice (LC 80).
- Remove a specific value (LC 27).
- What if the array isn't sorted? (Then a Set is the natural fit.)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the slow pointer at slow=1 not slow=0?
Element 0 is always unique by itself, so it stays put. We start writing from index 1 onward.
Why not just use a Set?
A Set works for the unsorted version (O(n) time and space). For the sorted version, the two-pointer is O(1) space — that's what the in-place wording is grading.
Practice these live with InterviewChamp.AI
Drill Remove Duplicates from Sorted Array and other ServiceNow interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →