forked from sunstick/code-street
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_dup_array.cpp
More file actions
47 lines (38 loc) · 1.11 KB
/
remove_dup_array.cpp
File metadata and controls
47 lines (38 loc) · 1.11 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int newn = 0;
int i = 0;
int j = 0;
while (j < n) {
A[i++] = A[j];
newn++;
while (j < n && A[j] == A[i - 1]) {
j++;
}
}
return newn;
}
};
int removeDuplicates(int A[], int n) {
int tail = 0;
for (int i = 0; i < n; i++) {
// invariant: A[0 ... tail - 1] is the array that contains all elements in A[0 ... i] without duplicates
int j;
for (j = 0; j < tail; j++)
if (A[j] == A[i])
break;
if (j == tail)
A[tail++] = A[i];
}
return tail;
}