> For the complete documentation index, see [llms.txt](https://code-snippets.hbamithkumara.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://code-snippets.hbamithkumara.com/leetcode/problems/1-100/remove-duplicates-from-sorted-array-ii.md). Agents can also search and read this documentation through its [MCP server](https://code-snippets.hbamithkumara.com/~gitbook/mcp).

# 80. Remove Duplicates from Sorted Array II

### Description

Given a sorted array *nums*, remove the duplicates [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) such that duplicates appeared at most *twice* and return the new length.

Do not allocate extra space for another array, you must do this by **modifying the input array** [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) with O(1) extra memory.

### **Constraints**

### **Approach**

### Links

* GeeksforGeeks
* [Leetcode](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)
* [ProgramCreek](https://www.programcreek.com/2013/01/leetcode-remove-duplicates-from-sorted-array-ii-java/)
* YouTube

### Examples

{% tabs %}
{% tab title="Example 1" %}
Given 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** respectively.

It doesn't matter what you leave beyond the returned length.
{% endtab %}

{% tab title="Example 2" %}
Given nums = **\[0, 0, 1, 1, 1, 1, 2, 3, 3]**,

Your function should return length = **7**, with the first seven elements of nums being modified to **0, 0, 1, 1, 2, 3** and **3** respectively.

It doesn't matter what values are set beyond the returned length.
{% endtab %}
{% endtabs %}

### Solutions

{% tabs %}
{% tab title="Solution 1" %}

```java
/**
 * Time complexity : O(n) since we process each element exactly once.
 * Space complexity : O(1)
 */

class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums == null || nums.length == 0) return 0;
        int j = 1, count = 1;
        for(int i = 1; i < nums.length; i++) {
            if(nums[i-1] == nums[i]) {
                count++;
            } else {
                count = 1;
            }
            if(count <= 2) nums[j++] = nums[i];
        }
        return j;
    }
}
```

{% endtab %}

{% tab title="Solution 2" %}

```java
/**
 * Time complexity : O(n) since we process each element exactly once.
 * Space complexity : O(1)
 */

class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums==null) return 0;
        if(nums.length <= 2) {
            return nums.length;
        }
        int i = 1, j = 2;
        while (j < nums.length) {
            if(nums[j] == nums[i] && nums[j] == nums[i-1]) {
                j++;
            } else {
                i++;
                nums[i] = nums[j];
                j++;
            }
        }
        return i + 1;
    }
}
```

{% endtab %}
{% endtabs %}

### **Follow up**

*
