# Remove Duplicates from Sorted Array

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

## Loop invariant

If there is `n`

joint of different number in the array (`A[i] != A[i - 1]`

```
),
the length of the final array without duplicates should be
```

n + 1`.

set a wall such that the numbers on the left of the wall are unique.

if we found a joint, increase the wall and move the unique number onto the left side of the wall.

```
[1,1,2]
w
```

joint found, `2 != 1`

, move `2`

to the left of the wall.

```
[1,2,2]
w
```

return wall's length

### 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 for(int i = 1; i< A.length; i++){
9 if(A[i] != A[i - 1]) {
10 for(int j = i - 1 ; j > len - 1 ; j-- )
11 A[j] = A[i];
12
13 len++;
14 }
15 }
16
17 return len;
18 }
19 }
```