Created
February 6, 2014 07:14
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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