The Indian Engineer

Problem 80 Remove Duplicates from Sorted Array II

Posted on 6 mins

Array Two-Pointers

Problem Statement

Link - Problem 80

Question

Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

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.

 

Example 1:

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

 

Constraints:

Solution

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int count = 1;
        for(int i=1;i<nums.size();i++){
            if(nums[i]==nums[i-1])
            count++;
            else if(nums[i]!=nums[i-1])
            count = 1;
            if(count>2)
            {nums.erase(nums.begin()+i);
            i--;}
        }
        return nums.size();
    }
};

Complexity Analysis

| Algorithm                      | Time Complexity | Space Complexity |
| ------------------------------ | --------------- | ---------------- |
| Modified Two-pointer traversal | O(n)            | O(1)             |

Explanation

Intial Thoughts

To approach this problem, think of it like organizing a shelf with books of different titles. Each title can have at most two copies. Start by looking at the books one by one. If a book is the same as the previous one, increment a counter. If a book is different, reset the counter. This process helps identify which books to keep and which to remove. Consider how modifying the shelf (or array) in-place affects the overall organization. It’s also important to keep track of the number of books (or elements) that are kept after the duplicates are removed.

Intuitive Analysis

Imagine walking through a sorted list of numbers, similar to walking through a library with books arranged alphabetically. The goal is to leave behind a list where each unique number appears no more than twice. Think of using pointers or markers to keep track of positions in the list where changes need to be made. Incrementing a pointer when a number can be added, and considering the count of each number to decide whether to include it or not. The key intuition is to maintain a pointer that always points to the next position to write to, effectively overwriting redundant elements. This method ensures that the relative order is preserved and that each unique number appears at most twice.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.


Similar Questions:

Title URL Difficulty
Remove Duplicates from Sorted Array https://leetcode.com/problems/remove-duplicates-from-sorted-array Easy