Skip to main content

4. Remove Duplicates from Sorted Array

easyAsked at Workday

Remove duplicates in-place from a sorted array. Workday tests this for two-pointer fluency — they dedup employee IDs in payroll batches that arrive from multiple HRIS feeds.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday SDE intern screen.
  • Blind (2026-Q1)Workday Pleasanton phone screen.

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 k, the number of unique elements.

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • -100 <= nums[i] <= 100
  • nums is sorted in non-decreasing order.

Examples

Example 1

Input
nums = [1,1,2]
Output
2, nums = [1,2,_]

Explanation: Function should return k = 2, with the first two elements being 1 and 2.

Example 2

Input
nums = [0,0,1,1,1,2,2,3,3,4]
Output
5, nums = [0,1,2,3,4,_,_,_,_,_]

Approaches

1. Set + rebuild

Convert to set, write back into array.

Time
O(n)
Space
O(n)
const uniq = [...new Set(nums)];
for (let i=0;i<uniq.length;i++) nums[i]=uniq[i];
return uniq.length;

Tradeoff: Works but allocates auxiliary space — the problem explicitly wants in-place.

2. Two pointers (write/read)

Slow pointer = next write slot; fast pointer scans. Only write when nums[fast] != nums[slow-1].

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: In-place, single pass. The slow/fast naming makes the invariant obvious to the interviewer.

Workday-specific tips

Workday grades for the two-pointer pattern AND that you preserve the suffix-doesn't-matter contract (only positions [0..k) need to be correct). Don't waste time clearing trailing values unless the interviewer asks.

Common mistakes

  • Starting slow at 0 instead of 1 — overcomplicates the comparison logic.
  • Comparing nums[fast] to nums[slow] instead of nums[fast-1] — only works because sorted, but the intent is muddled.
  • Returning the array instead of k — the prompt specifies k.

Follow-up questions

An interviewer at Workday may pivot to one of these next:

  • Remove Duplicates from Sorted Array II (LC 80) — allow at most 2.
  • Remove Element (LC 27) — remove a specific value in-place.
  • What if the array is unsorted?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why don't I need to clear the tail?

The contract says only the first k positions must be correct; the rest are 'don't care'. Clearing wastes O(n) work for zero benefit.

What if the input isn't sorted?

Then you'd need a hash set — different problem, O(n) extra space.

Practice these live with InterviewChamp.AI

Drill Remove Duplicates from Sorted Array and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →