# remove duplicates from sorted array I&II

## remove duplicates from sorted array I&II

### I not hard

Use two pointer `i = 0` and `j = 1`.If they are the same , point`j` to the next. Otherwise, put `A[j]` to the next of `i`, then point `j` to the next.

`````` 1 class Solution {
2 public:
3     int removeDuplicates(int A[], int n) {
4         if ( n <= 1 ) {
5             return n;
6         }
7         int i = 0;
8         int j = 1;
9         while ( j < n ) {
10             if ( A[i] == A[j] ) {
11                 j++;
12             } else {
13                 A[++i] = A[j++];
14             }
15         }
16         return i+1;
17     }
18 };``````

### II not hard too

Use some a valiable to record the times `A[i]` and `A[j]` are same.Then do the same thing.Attention that when they are same and mark is not reach 1, donot forget set `A[++i]` with `A[j++]`.

`````` 1 class Solution {
2 public:
3     int removeDuplicates(int A[], int n) {
4         if ( n <= 2 ) {
5             return n;
6         }
7         int mark = 0;
8         int i = 0;
9         int j = 1;
10         while ( j < n ) {
11             if ( A[i] == A[j] ) {
12                 if ( mark < 1 ) {
13                     A[++i] = A[j++];
14                     mark++;
15                 } else {
16                     j++;
17                 }
18             } else {
19                 A[++i] = A[j++];
20                 mark = 0;
21             }
22         }
23         return i+1;
24     }
25 };``````