-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_diff.c
84 lines (60 loc) · 1.49 KB
/
array_diff.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
array_diff.c
Exercício do CodeWars:
Your goal in this kata is to implement a difference function,
which subtracts one list from another and returns the result.
It should remove all values from list a, which are present in list b.
array_diff({1, 2}, 2, {1}, 1, *z) == {2} (z == 1)
If a value is present in b, all of its occurrences must be removed from the other:
array_diff({1, 2, 2, 2, 3}, 5, {2}, 1, *z) == {1, 3} (z == 2)
*/
#include <stdio.h>
#include <stdlib.h>
int *array_diff(const int *arr1, int n1, const int *arr2, int n2, int *z);
int main(){
//int a1[2] = {1,2};
//int a2[1] = {1};
//int a1[5] = {1, 2, 2, 2, 3};
//int a2[1] = {2};
//int a1[3] = {1,2,2};
//int a2[1] = {1};
//int a1[3] = {1,2,2};
//int a2[1] = {};
//int a1[3] = {1,2,2};
//int a2[0] = {};
//int a1[0] = {};
//int a2[2] = {1,2};
int a1[5] = {1,2,3,4,5};
int a2[3] = {1,3,4};
int result[3] = {0};
int *t;
int *r;
int i;
r = array_diff(a1,5,a2,3,result);
printf("%d \n",*r);
for(i=0;i<*r;i++){
printf("%d ",result[i]);
}
return 0;
}
int *array_diff(const int *arr1, int n1, const int *arr2, int n2, int *z) {
int nr=0;
int c = 0;
int exist = 0;
int i,j;
int *index;
for(i=0;i<n1;i++){
c = arr1[i];
for(j=0;j<n2;j++){
if(c == arr2[j]) exist=1;
}
if(!exist){
z[nr] = c;
nr++;
}
exist=0;
}
index = &nr;
return index;
}