Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Remove Duplicates from Sorted Array

XPChallenge Points: 10
levelLevel: Easy

You are given a sorted integer array nums in non-decreasing order.
Remove the duplicate elements in-place so that each unique element appears only once.

  • The relative order of the elements must be maintained.
  • Return the number of unique elements in the array.

Notes:

  • Modify nums such that the first k elements contain the unique elements in their original order.
  • It does not matter what values appear after the first k elements.
  • Return k, the number of unique elements.
Example 1:

Input: nums = [2,2,3,4]

Output: 3, nums = [2,3,4,_]

Explanation:

There are three unique elements: 2, 3, 4. The remaining element after the first three does not matter.

Example 2:

Input: nums = [1,1,1,2,2,3]

Output: 3, nums = [1,2,3,_,_,_]

Explanation:

The unique elements are 1, 2, 3. Remaining values can be anything.

Example 3:

Input: nums = [0,1,1,2,2,2,3,3,4,4]

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

Explanation:

The unique elements are 0, 1, 2, 3, 4.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result