# Remove Duplicates from Sorted Array II

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii

## Improved version of Remove Duplicates from Sorted Array

The difference is that how much the wall should be increased.

if the number is not seen before, the wall should be increased `1`

in size.
if the number is seen more than 1 time, the wall should be increased `2`

in size.

As all the numbers are sorted, one variable is enough to count the number ever seen before.

And the wall should be increased `MIN(lastseencount, 1) + 1`

### Source code *Read on Github*

```
1 public class Solution {
2 public int removeDuplicates(int[] A) {
3 // Note: The Solution object is instantiated only once and is reused by each test case.
4
5 if(A.length == 0) return 0;
6
7 int len = 1;
8 int lastseencount = 0;
9
10 for(int i = 1; i< A.length; i++){
11 if(A[i] != A[i - 1]){
12
13 for(int j = i - 1; j > len + Math.min(lastseencount, 1) - 1 ; j--)
14 A[j] = A[i];
15
16 len += Math.min(lastseencount, 1) + 1;
17 lastseencount = 0;
18 }else{
19 lastseencount++;
20 }
21 }
22
23 return len + Math.min(lastseencount, 1);
24 }
25 }
```