-----QUESTION-----
Given a sorted array, remove the duplicates in place such that each element appear only
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2]
,
Your function should return length = 2
, and A is now [1,2]
.
-----SOLUTION-----
class Solution {
public:
int removeDuplicates(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n==0) return NULL;
int start = 0;
for (int i = 1; i< n; i++)
{
if(A[start] != A[i])
{
A[++start] = A[i];
}
}
return start+1;
}
};