-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremel.c
55 lines (43 loc) · 882 Bytes
/
remel.c
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
48
49
50
51
52
53
54
55
/*
* remove duplicate element in the arry
* key is "shift"
* Input is always ordered
*/
#include <stdio.h>
#include <stdlib.h>
void list(int *arr, int size) {
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
void shift(int *arr, int size) {
int i;
for (i = 0; i < size-1; i++) {
arr[i] = arr[i+1];
}
}
int removeDuplicates(int *nums, int numsSize) {
int i, j;
int returnSize = numsSize;
for (i = 0; i < numsSize; i++) {
for (j = i+1; j < numsSize; j++) {
if (nums[i] == nums[j]) {
shift(&nums[j], numsSize-j);
numsSize--;
j--;
}
printf("%d(%d): ", j, numsSize);
list(nums, numsSize);
}
}
printf("%d = returnSize\n", numsSize);
return numsSize;
}
int main(void) {
int nums[11] = { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 };
int size = 0;
size = removeDuplicates(nums, 11);
list(nums, size);
return 0;
}