Remove Duplicates from Sorted Array LeetCode Solution

Last updated on July 6th, 2024 at 03:59 am

Here, We see Remove Duplicates from Sorted Array Solution. This Leetcode problem is done in many programming languages like 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

Remove Duplicates from Sorted Array LeetCode Solution

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

1. Remove Duplicates from Sorted Array Leetcode Solution C++

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

1.1 Explanation

  1. The std::unique function rearranges the elements in the range [nums.begin(), nums.end()) such that each unique element appears only once, and returns an iterator to the element that follows the last unique element.
  2. The nums.erase function is then used to remove the redundant elements from the vector.
  3. Finally, the size of the vector, which now contains only unique elements, is returned.

1.2 Time Complexity

O(n), where n is the number of elements in the vector. This is because std::unique and erase both operate in linear time.

1.3 Space Complexity

O(1), as the operation is done in place without using additional space.

2. Remove Duplicates from Sorted Array Leetcode Solution Java

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

2.1 Explanation

  1. The function checks if the array is empty and returns 0 if true.
  2. It initializes a variable i to track the position of unique elements.
  3. It iterates through the array using a second variable j. If it finds a new unique element, it increments i and copies the unique element to the position i.
  4. Finally, it returns i + 1, which is the count of unique elements in the array.

2.2 Time Complexity

O(n), where n is the number of elements in the array.

2.3 Space Complexity

O(1), as it modifies the array in place without using additional space.

3. Remove Duplicates from Sorted Array Leetcode Solution JavaScript

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

3.1 Explanation

  1. The function initializes a counter c to track the position of unique elements.
  2. It iterates through the array. If the current element is the same as the previous one, it continues to the next iteration.
  3. If it finds a unique element, it assigns it to the position c and increments c.
  4. After the loop, it removes the redundant elements from the end of the array using the pop method.
  5. Note: This implementation modifies the array in place and returns no value. To match the behavior of other implementations, it could return c at the end.

3.2 Time Complexity

O(n), where n is the number of elements in the array.

3.3 Space Complexity

O(1), as it modifies the array in place without using additional space.

4. Remove Duplicates from Sorted Array Leetcode Solution 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_

4.1 Explanation

  1. The function initializes len_ to track the position of unique elements.
  2. It checks if the list is empty and returns 0 if true.
  3. It iterates through the list starting from the second element. If it finds a unique element, it assigns it to the position len_ and increments len_.
  4. Finally, it returns len_, which is the count of unique elements in the list.

4.2 Time Complexity

O(n), where n is the number of elements in the list.

4.3 Space Complexity

O(1), as it modifies the list in place without using additional space.

Scroll to Top