NLeetCode Solutions
ProblemsPatternsStatistics
© 2026 Created by Trịnh Minh Nhật
GitHubLeetCode
Related posts
  • 27. Remove ElementEasy
  • 283. Move ZeroesEasy
  • 1. Two SumEasy
  • 14. Longest Common PrefixEasy
  • 49. Group AnagramsMedium

26. Remove Duplicates from Sorted Array

Easy
ArrayTwo Pointers
Answered: Dec 10, 2022
View on LeetCode

Problem Description

Given an integer 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.

Consider the number of unique elements in nums to be k​​​​​​​. After removing duplicates, return the number of unique elements k.

The first k elements of nums should contain the unique numbers in sorted order. The remaining elements beyond index k - 1 can be ignored.

Custom Judge:

The judge will test your solution with the following code:

int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length

int k = removeDuplicates(nums); // Calls your implementation

assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
    assert nums[i] == expectedNums[i];
}

If all assertions pass, then your solution will be accepted.

Examples

Example 1

Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Explanation:
Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2

Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Explanation:
Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).

Constraints

  • 1 <= nums.length <= 3 * 104
  • -100 <= nums[i] <= 100
  • nums is sorted in non-decreasing order.

💡 Hints (3)


Solutions

Complexity Analysis

Time Complexity:O(n)

We iterate through the array once, where n is the length of the input array. Each element is visited exactly once.

Space Complexity:O(1)

We only use a constant amount of extra space for the pointer k, regardless of the input size.

Since the array is sorted, all duplicate elements are consecutive. We use a two-pointer approach where k tracks the position to place the next unique element. We iterate through the array with a pointer i, and whenever we encounter an element different from the previous one, we know it's unique—we copy it to position k and increment k. After iterating through the entire array, k represents the count of unique elements.

Complexity Analysis

Time Complexity:O(n)

We iterate through the array once with pointer j, where n is the length of the input array. Each element is examined exactly once.

Space Complexity:O(1)

We use constant extra space for the two pointers i and j, independent of the input size.

This is a variant of the two-pointer approach using explicit pointers i and j. Pointer j iterates through the array while pointer i tracks where the next unique element should be placed. When j encounters an element different from its predecessor, it's unique—we copy it to position i and advance both pointers. This explicit two-pointer pattern is slightly more readable and mirrors the intuitive thinking of having one pointer to read and one to write.

Conclusion

ApproachRatingTime ComplexitySpace ComplexityAdvantagesDisadvantages
Two Pointers - Simple
O(n)O(n)O(n)O(1)O(1)O(1)Concise and straightforward with implicit pointer trackingImplicit pointer may be less intuitive for beginners
Two Pointers - Explicit
O(n)O(n)O(n)O(1)O(1)O(1)Clear separation of read and write pointersSlightly more verbose than Solution 1

The optimal approach for this problem is Solution 1: Two Pointers - Simple, which achieves O(n)O(n)O(n) time complexity and O(1)O(1)O(1) space complexity. This solution is the most elegant and efficient, using a minimalist two-pointer pattern that directly solves the in-place removal requirement.


Previous20. Valid Parentheses
Next27. Remove Element
Related posts
  • 27. Remove ElementEasy
  • 283. Move ZeroesEasy
  • 1. Two SumEasy
  • 14. Longest Common PrefixEasy
  • 49. Group AnagramsMedium
On this page
15 lines
1class Solution:
2 def removeDuplicates(self, nums: List[int]) -> int:
3 if not nums:
4 return 0
5
6 k = 1 # Start from the second element because the first element is always unique
7
8 for i in range(1, len(nums)):
9 # If the current element is different from the previous one, it's a unique element
10 if nums[i] != nums[i - 1]:
11 # Copy the unique element to the position k and increment k
12 nums[k] = nums[i]
13 k += 1
14
15 return k
19 lines
1class Solution:
2 def removeDuplicates(self, nums: List[int]) -> int:
3 # Edge case: If the input array is empty, return 0
4 if not nums:
5 return 0
6
7 # Initialize two pointers: i for the position of the next unique element, and j for iterating through the array
8 i = 1
9 j = 1
10
11 while j < len(nums):
12 # If the current element is different from the previous one, it's a unique element
13 if nums[j] != nums[j - 1]:
14 nums[i] = nums[j]
15 i += 1
16
17 j += 1
18
19 return i