-----QUESTION-----
Follow up for "Remove Duplicates":
What if duplicates are allowed at most
For example,
Given sorted array A = [1,1,1,2,2,3]
,
Your function should return length = 5
, and A is now [1,1,2,2,3]
.
-----SOLUTION-----
class Solution {
public:
int removeDuplicates(int A[], int n) {
if(n==0) return NULL;
int start = 0;
for (int i = 1; i< n; i++)
{
if(A[start] != A[i] || ( A[start] == A[i] && i+1 < n && A[start] != A[i+1])
|| (A[start] == A[i] && i+1 >=n))
{
A[++start] = A[i];
}
}
return start+1;
}
};