Remove Duplicates from Sorted Array LeetCode Solution

Last updated on January 13th, 2025 at 10:03 pm

Here, We see Remove Duplicates from Sorted Array Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.

List of all LeetCode Solution

Topics

Array, Two-pointers

Companies

Bloomberg, Facebook, Microsoft

Level of Question
Remove Duplicates from Sorted Array LeetCode Solution

1. Problem Statement

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

Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:

Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.
Return k.

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).

2. Coding Pattern Used in Solution

The coding pattern used in this problem is Two Pointers.

  • Why Two Pointers?
    • One pointer keeps track of the position where the next unique element should be placed.
    • The other pointer iterates through the array to find unique elements.
    • This approach ensures that the array is processed in a single pass, making it efficient.

3. How the Code Works

  1. The input array is sorted, so duplicates are always adjacent.
  2. A pointer is used to track the position of the last unique element.
  3. A loop iterates through the array, comparing the current element with the previous one:
    • If the current element is different, it is placed at the position tracked by the pointer.
    • The pointer is then incremented.
  4. At the end of the loop, the pointer indicates the length of the array with unique elements.
  5. In the C++ and JavaScript versions, additional steps are taken to remove the extra elements beyond the new length.

4. Code Implementation in Different Languages

4.1 Remove Duplicates from Sorted Array C++

class Solution {
public:
    int removeDuplicates(vector& nums) {
        nums.erase(std::unique(nums.begin(), nums.end()), nums.end());
        return nums.size();
    }
};

4.2 Remove Duplicates from Sorted Array Java

class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums.length == 0) return 0;
    int i = 0;
    for (int j = 1; j < nums.length; j++) {
        if (nums[j] != nums[i]) {
            i++;
            nums[i] = nums[j];
        }
    }
    return i + 1;
    }
}

4.3 Remove Duplicates from Sorted Array JavaScript

var removeDuplicates = function(nums) {
    let c=1
    for(let i=1;i<nums.length;i++){
        if(nums[i]==nums[i-1])continue
        nums[c]=nums[i]
        c++
    }
    let l=nums.length-1
    while(l>=c){
        nums.pop()
        l--
    }
};

4.4 Remove Duplicates from Sorted Array Python

class Solution:
    def removeDuplicates(self, nums):
        len_ = 1
        if len(nums) == 0:
            return 0
        for i in range(1, len(nums)):
            if nums[i] != nums[i-1]:
                nums[len_] = nums[i]
                len_ += 1
        return len_

5. Time and Space Complexity

Time ComplexitySpace Complexity
C++O(n)O(1)
JavaO(n)O(1)
JavaScriptO(n)O(1)
PythonO(n)O(1)
  • The Two Pointers pattern is ideal for problems involving sorted arrays where in-place modifications are required.
  • All implementations achieve the same time and space complexity, but the C++ and JavaScript versions include additional steps to remove extra elements beyond the new length.
  • The algorithm is efficient, with linear time complexity and constant space usage.
Scroll to Top