The following is my C++ code. While I am not sure if it is working correctly, I don't understand why I kept getting **Output Limit Exceeded**. Any suggestions would be appreciated. Many thanks!

```
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n == 0) return 0;
int i = 1, j = 1;
while (i < n) {
if (A[i] == A[i - 1]) {
j = i;
while (A[j] == A[i] && j < n) j++;
if (j < n) {
A[i] = A[j];
} else {
break;
}
}
i++;
}
return i;
}
};
```