12 · Two Pointers

#26 Remove Duplicates from Sorted Array

Easysolve on LeetCode ↗

frequently reported atBloombergMicrosoftMetaGoogleAmazonTCS

the problem

In plain words

Remove duplicates in place from a sorted array so each unique value appears once, keeping order. Return k, the number of unique values; the first k slots must hold them.

in nums = [1,1,2]

out 2, nums = [1,2,_]

in nums = [0,0,1,1,1,2,2,3,3,4]

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

  • 1 ≤ n ≤ 3·10⁴
  • Sorted non-decreasing

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #80
    2. #27
      Remove Element
      Easyin Two Pointers
    3. #283
      Move Zeroes
      Easyin Two Pointers