Skip to main content

4. Remove Duplicates from Sorted Array

easyAsked at Asana

Remove duplicates from a sorted array in-place and return the new length. Asana likes this to check whether you handle the two-pointer pattern cleanly — a precursor to their dedup steps in activity-feed merge logic.

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

Source citations

Public interview reports confirming this problem appears in Asana loops.

  • Glassdoor (2026-Q1)Asana new-grad 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 after placing the final result in the first k slots of nums.

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,_]

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 + rewrite

Dump into a Set, sort, write back.

Time
O(n log n)
Space
O(n)
function removeDuplicates(nums) {
  const u = [...new Set(nums)].sort((a,b)=>a-b);
  for (let i=0;i<u.length;i++) nums[i] = u[i];
  return u.length;
}

Tradeoff: Ignores the sorted-input freebie and uses O(n) extra space.

2. Two pointers in-place

Write pointer w advances only when we see a new value. Read pointer r scans.

Time
O(n)
Space
O(1)
function removeDuplicates(nums) {
  if (nums.length === 0) return 0;
  let w = 1;
  for (let r = 1; r < nums.length; r++) {
    if (nums[r] !== nums[r - 1]) {
      nums[w++] = nums[r];
    }
  }
  return w;
}

Tradeoff: O(1) extra space, single pass. The sortedness guarantee is what makes the duplicate detection O(1) per element.

Asana-specific tips

Asana grades on whether you exploit the sorted invariant — candidates who reach for Set first reveal they're not reading the constraints. State explicitly 'because the array is sorted, duplicates are adjacent, so I only need to compare to the previous kept value.'

Common mistakes

  • Comparing nums[r] to nums[r-1] in the read array but accidentally writing over the comparison.
  • Returning the array instead of the length k.
  • Using nums.length as the loop bound after writing — should use the original length.

Follow-up questions

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

  • Allow each element to appear at most twice (LC 80).
  • What if the array isn't sorted?
  • Generalize to 'at most k duplicates allowed'.

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 does the in-place version not need to compare with nums[w-1]?

Either works. Comparing with nums[r-1] is simpler because r advances every iteration, while w only advances on writes.

Does the test harness check both k and the array contents?

Yes — LC and most Asana harnesses check the first k slots of nums match the expected unique sequence.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →