forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveDuplicatesFromSortedArray.cpp
More file actions
34 lines (30 loc) · 949 Bytes
/
removeDuplicatesFromSortedArray.cpp
File metadata and controls
34 lines (30 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Source : https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/
// Author : Hao Chen
// Date : 2014-06-22
/**********************************************************************************
*
* 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.
*
* For example,
* Given input array A = [1,1,2],
*
* Your function should return length = 2, and A is now [1,2].
*
*
**********************************************************************************/
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n<=1) return n;
int pos=0;
for(int i=0; i<n-1; i++){
if (A[i]!=A[i+1]){
A[++pos] = A[i+1];
}
}
return pos+1;
}
};