Back to Dashboard

Find Non-Duplicate Number Instances

Easy

Problem Statement

Given an 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. Then return the number of unique elements in nums.

Examples

Example 1:

  • Input: nums = [1,1,2]
  • Output: 2

Example 2:

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

Approach 1: Two Pointers

O(n)
O(1)
class Solution {
    public int removeDuplicates(int[] nums) {
        var p1 = 0;
        var p2 = 0;
        while (p2 < nums.length - 1) {
            if (nums[p2] == nums[p2 + 1]) {
                p2 ++;
                continue;
            }
            p1 ++;
            p2 ++;
            nums[p1] = nums[p2];
        }
        return p1 + 1;
    }
}

Status

Solved

Complexity

Time
O(n)
Space
O(1)

Tags

Array

Date

2026-02-07
View Problem Source