Skip to main content

4. Remove Duplicates from Sorted Array

easyAsked at Intuit

Given a sorted array, remove duplicates in-place and return the new length. Intuit asks this in dedup contexts — TurboTax cleans duplicated 1099 rows, QuickBooks dedupes bank-feed transactions before reconciliation.

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

Source citations

Public interview reports confirming this problem appears in Intuit loops.

  • Glassdoor (2025-11)Intuit reconciliation team SWE screen, framed as deduplicating a sorted transaction list.
  • LeetCode Discuss (2026-Q1)TurboTax internship phone screen reported in 2026-Q1 thread.

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. The first k slots of nums must hold the unique values; the rest may be anything.

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 returns 2; first two slots are unique values.

Example 2

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

Approaches

1. Allocate a new array

Build a new array of uniques by scanning and skipping equal-to-last.

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: Linear time but uses O(n) extra space — violates the in-place spirit of the prompt.

2. Two-pointer in-place (optimal)

Slow pointer marks the next unique slot. Fast pointer scans. When nums[fast] != nums[slow-1], copy 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: Single pass, O(1) extra space. Works in-place. The slow pointer doubles as the result count.

Intuit-specific tips

Intuit interviewers grade this on whether you treat 'sorted' as a useful property — dedup of unsorted requires a hash set, dedup of sorted is two-pointer. They want to hear you say that aloud. Bonus signal: connect to bank-feed dedup where the sort key is (date, amount, descriptor) and 'equal' means tuple-equal, not value-equal.

Common mistakes

  • Returning the array instead of its length — LC's harness expects the length.
  • Forgetting the nums.length === 0 guard — accessing nums[0] on empty throws.
  • Using a hash set on already-sorted input — works but loses the O(1) space win.

Follow-up questions

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

  • Remove Duplicates from Sorted Array II (LC 80) — allow at most 2 of each.
  • Dedupe by composite key (date, amount, memo) instead of single value.
  • Dedupe a sorted linked list (LC 83).

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 problem say 'in-place' so strongly?

In production reconciliation engines, arrays of transactions can be millions of rows. Allocating a parallel array doubles memory pressure; in-place doesn't.

How would this change if input weren't sorted?

You'd need a Set or a hash, costing O(n) extra space. The two-pointer approach only works because adjacency = equality.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →