Skip to content

Instantly share code, notes, and snippets.

Created February 6, 2014 07:14
Show Gist options
  • Save anonymous/8839605 to your computer and use it in GitHub Desktop.
Save anonymous/8839605 to your computer and use it in GitHub Desktop.
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory.
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n == 0) return 0;
int i = 0;
int j = 1;
for (; j < n; j++) {
if (A[j] != A[i]) A[++i] = A[j];
}
return i + 1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment