DSA two pointers learning path Level 1 This level · 4 of 5
Easy dsa two-pointers

Remove Duplicates from Sorted Array

The 30 second answer

Because the array is sorted, duplicates sit next to each other. I keep a write index for the next unique value and scan from the left. Whenever the read value differs from the last unique one I write it forward and advance the write index. I return that index as the new length. Linear time, constant extra space.

Could you say this in an interview?

Problem Given a sorted integer array, remove the duplicates in place so that each unique value appears once, keeping the original order. Return the count k of unique values. The first k slots of the array must hold those values; anything past k is ignored. Original: LeetCode 26: Remove Duplicates from Sorted Array.

Understanding the prompt You do not need to shrink the array object. The judge only reads nums[0..k). Because the input is sorted, equal values are adjacent, so a single previous-value check is enough.

Pattern recognition Read/write pointers that compact the array in place under a keep-or-skip rule.

Brute force Copy unique values into a set and write them back: O(n) extra space, and a HashSet would also lose the given order unless you use LinkedHashSet.

Optimal Start with w = 1. For each later index r, if nums[r] differs from nums[w - 1], write it at w and increment w. Return w. An empty array returns 0.

Alternatives If the array were unsorted, you would sort first or use a set. The follow-up that allows each value at most twice is the same loop with a slightly looser comparison (nums[r] != nums[w - 2]).

Java solution

import java.util.Arrays;

public class RemoveDuplicatesFromSortedArray {
    static int removeDuplicates(int[] nums) {
        if (nums.length == 0) return 0;
        int w = 1;
        for (int r = 1; r < nums.length; r++) {
            if (nums[r] != nums[w - 1]) nums[w++] = nums[r];
        }
        return w;
    }

    public static void main(String[] args) {
        var a = new int[] {1, 1, 2};
        int k = removeDuplicates(a);
        System.out.println(k + ""+ Arrays.toString(Arrays.copyOf(a, k))); // 2 [1, 2]
        var b = new int[] {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
        k = removeDuplicates(b);
        System.out.println(k + ""+ Arrays.toString(Arrays.copyOf(b, k))); // 5 [0, 1, 2, 3, 4]
    }
}

Complexity O(n) time, O(1) extra space.

Edge cases

  • All values equal: the answer is 1.
  • Already unique: the write pointer stays just behind the read pointer and the array is unchanged.
  • An empty array.

Follow-ups

  • Each unique value may appear at most twice (LeetCode 80).
  • Remove a specific value in place (LeetCode 27): same write pointer, different predicate.

Tests [1,1,2] returns 2 and the prefix [1,2]. [0,0,1,1,1,2,2,3,3,4] returns 5 and the prefix [0,1,2,3,4].

How to say it in the interview "The array is sorted, so duplicates are neighbours. I keep a write index of the unique prefix. When the next value differs from the last unique one, I copy it forward. I return the write index. Linear time, constant space."

Sources (2)
  1. LeetCode 26: Remove Duplicates from Sorted Arrayleetcode.com
  2. Java SE 17 API: Arrays.copyOfdocs.oracle.com
Previous
Next