forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path27.c
More file actions
34 lines (29 loc) · 632 Bytes
/
27.c
File metadata and controls
34 lines (29 loc) · 632 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
#include <stdio.h>
int removeElement(int A[], int n, int elem) {
int i, j, len;
len = n;
i = 0, j = n - 1;
while (i < len) {
if (A[i] == elem) {
int t = A[j];
A[j] = A[i];
A[i] = t;
len--;
j--;
}
else {
i++;
}
}
return len;
}
int main() {
int A[] = { 3, 1, 2, 3, 3, 4, 5, 3, 6 };
int new_len = removeElement(A, sizeof(A) / sizeof(A[0]), 3);
int i;
for (i = 0; i < new_len; i++) {
printf("%d ", A[i]);
}
printf("\n");
return 0;
}