Skip to main content

4. Remove Duplicates from Sorted Array

easyAsked at Salesforce

Remove duplicates from a sorted array in place and return the new length. Salesforce asks this to verify you can manage two pointers cleanly — they use the same pattern in their dedup logic for record imports.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Data Cloud team uses this as a Bulk API dedup analogue.
  • Reddit r/cscareerquestions (2025)Cited as the easy warmup before a hash-based dedup follow-up.

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. Modify the first k elements of nums to be the 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; first 2 elements should be 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. Use a Set

Build a Set from the array, then copy back; not in-place.

Time
O(n)
Space
O(n)
function removeDuplicates(nums) {
  const unique = [...new Set(nums)];
  for (let i = 0; i < unique.length; i++) nums[i] = unique[i];
  return unique.length;
}

Tradeoff: Violates the in-place requirement and uses O(n) extra space. Salesforce explicitly fails this.

2. Two-pointer write index

Keep a 'write' pointer at position 1. Walk with a 'read' pointer; when nums[read] differs from nums[write-1], write it.

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

Tradeoff: True in-place, O(1) extra space, and the sorted invariant means a single comparison suffices.

Salesforce-specific tips

Salesforce uses dedup logic heavily in record imports (Lead, Contact merges), so they grade this on whether you exploit the sorted invariant. Bonus signal: mention that this pattern generalizes to 'keep at most k duplicates' which is LC 80 and a Salesforce-favorite follow-up.

Common mistakes

  • Comparing nums[read] === nums[write] instead of nums[read - 1] — gives the wrong invariant after the first write.
  • Forgetting to handle the empty array — returns NaN or crashes.
  • Returning the array instead of the length — the contract is the length.

Follow-up questions

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

  • Allow each element to appear at most twice (LC 80).
  • What if the array is unsorted? (Hash set, O(n) extra space.)
  • Dedup a Salesforce Lead list by composite key (firstName + lastName + email).

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 is write initialized to 1, not 0?

The first element nums[0] is always kept (it's unique by definition), so the write pointer starts at the next slot.

What happens to the elements beyond k?

The problem says they don't matter — you just have to guarantee the first k are correct. Most graders ignore the tail.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →