Skip to main content

64. Remove Duplicates from Sorted Array II

mediumAsked at Snowflake

Remove duplicates from a sorted array so each value appears at most twice. Snowflake uses this to test the generalized 'allow k occurrences' pattern — useful for k-NN deduplication during data ingestion.

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

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2025-Q4)Snowflake ingestion-team uses this as a generalization of LC 26.
  • LeetCode Discuss (2025-09)Reported at Snowflake SDE-I screens.

Problem

Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same. Return the number of elements in nums after the modification.

Constraints

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

Examples

Example 1

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

Example 2

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

Approaches

1. Counter + write index

Track a counter for the current value; only write while counter <= 2.

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

Tradeoff: Works, two state vars. Mention as the explicit-counter version.

2. Compare to nums[write-2] (optimal)

Write the current value only if it differs from nums[write-2] (i.e., we already have <= 2 copies).

Time
O(n)
Space
O(1)
function removeDuplicates(nums) {
  let write = 0;
  for (const n of nums) {
    if (write < 2 || n !== nums[write - 2]) {
      nums[write++] = n;
    }
  }
  return write;
}

Tradeoff: Two lines of state instead of one counter. Generalizes trivially to 'at most k'.

Snowflake-specific tips

Snowflake interviewers want the nums[write-2] trick and to hear you generalize: 'compare to nums[write-k] for at-most-k'. Bonus signal: connect to k-NN deduplication — when ingesting time-series data with k-near-duplicates, the same pattern caps occurrences.

Common mistakes

  • Hardcoding 2 in multiple places; generalize via a constant.
  • Forgetting the write < 2 guard — out-of-bounds on the first two elements.
  • Using nums[read-2] instead of nums[write-2] — counts source duplicates, not output duplicates.

Follow-up questions

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

  • At most k occurrences (generalize).
  • Unsorted variant — needs a hash map.
  • How does Snowflake's k-NN dedup work during ingestion?

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 nums[write-2] and not nums[read-2]?

nums[read-2] counts duplicates in the input. nums[write-2] counts duplicates in the output. We want the output to have at most 2, so compare against what we've already written.

How does this generalize?

For 'at most k', replace 2 with k. The compare-to-nums[write-k] template works for any k >= 1.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →