> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/80-remove-duplicates-from-sorted-array-ii.md).

# 80 Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":\
What if duplicates are allowed at most twice?

For example,\
Given sorted array nums=`[1,1,1,2,2,3]`,

Your function should return length =`5`, with the first five elements of nums being`1`,`1`,`2`,`2`and`3`. It doesn't matter what you leave beyond the new length.

**Note:** Since I did not understand problem completely at first, I thought it was also worth mentioning here that the idea is credit the new size of `nums`in such a way so that who ever uses this function can call it, and then simply get the subvector `vector<int>(nums.begin(), nums.begin() + count)`.

I thought it was a pretty cool problem so I tried to optimize my solution until I reached the 90% performance threshold.

**Attempt 1:** O(n^2) time and O(n) space. Maintain a hash for the duplicates up to a count of 2, or otherwise erase the number from `nums`. This is a poor implementation because it does not use the fact that the array is *sorted*, and it calls erase on a contiguous data structure. Bad bad.

```cpp
int removeDuplicates(vector<int>& nums) {
    unordered_map<int, int> int_freq;
    int_freq.reserve(nums.size());

    for (int i = 0; i < nums.size(); i++) {
        if (int_freq.find(nums[i]) != int_freq.end()) {
            if (int_freq[nums[i]] <= 1) {
                int_freq[nums[i]]++;
            }
            else {
                nums.erase(nums.begin() + i);
                i--;
            }
        }
        else
            int_freq.insert({ nums[i], 1 });
    }

    return nums.size();
}
```

**Attempt 2:** O(2n) time and O(1) space. Iterate through the numbers and accept the ones that don't contain the same elements 2 indices before it.

```cpp
int removeDuplicates(vector<int>& nums) {
    int it = 0;
    for (int n : nums) {
        // before a duplicate is even possible or
        // if 2 elements before is smaller
        if (it < 2 || nums[it - 2] != n)
            nums[it++] = n;
    }
    return it;
}
```

**Attempt 3:** O(n) time and O(1) space. Same idea, just separate the logic to start at index 2, rather than zero.

```cpp
int removeDuplicates(vector<int>& nums) {
    int it = 2;
    const size_t size = nums.size();
    for (int j = it; j < size; j++) {
        int check = nums[j];
        if (nums[it - 2] != check)
            nums[it++] = check;
    }

    return it > nums.size() ? nums.size() : it;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/80-remove-duplicates-from-sorted-array-ii.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
