+ 1

WAP in C to find set difference of two arrays?

a = {1, 2, 3, 4, 5, 6} b={2, 4, 5} result = {1,3,6}

13th Jun 2018, 6:45 PM
Jai Kumar Garg
9 Respostas
+ 3
@ Jai Kumar Garg DISCLAIMER: I haven't used C in mannnnnnny years (2001-02ish), so I'm use to the more modern languages that handle annoying stuff for you. lol With that being said, here is a simple solution for this. It has room to be improved upon, such as having it check the size of both arrays and create the result array based upon a more dynamic size for it. This could be a really round-about way of doing this, but for the sake of your question, it'll suffice. https://code.sololearn.com/c2D2oQaI3f3r/#c #include <stdio.h> #include <stdbool.h> int main() { int arrOne[8] = {1,2,3,4,5,6,9,7}; // Our first array int arrTwo[3] = {2,4,5}; // Our second array int result[11]; // Where we'll store the results int resCounter = 0; // Result counter bool isSame = false; // Help track unique values // Outer loop will iterate through our first array for(int i = 0; i < (sizeof(arrOne)/sizeof(arrOne[0])); ++i) { // Inner loop will iterate through our sescond array for(int j = 0; j < (sizeof(arrTwo)/sizeof(arrTwo[0])); ++j) { // Compare value from arrOne against arrTwo // If it's the same as any, it's not unique if(arrOne[i] == arrTwo[j]){ isSame = true; break; } } // if isSame is false, it's unique. Store it. if(isSame == false){ // Store value to result array result[resCounter] = arrOne[i]; // Print unique value printf("%d ", result[resCounter]); // Increase result index by 1 ++resCounter; } else { isSame = false; // Reset our var for next iteration } } return 0; } :::: OUTPUT ::::::: 1 3 6 9 7
14th Jun 2018, 6:04 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 4
What language you workin with?
13th Jun 2018, 6:50 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
You can do it easily in ruby. a = [1,2,3,4,5,6] b = [2,4,5] result = a - b puts result Try it yourself in the Ruby's Code Playground.
13th Jun 2018, 6:52 PM
Akash Pal
Akash Pal - avatar
+ 2
int is_inset (int *a,int size_of_a,int bi){int i;for (i=0;i<size_of_a;i++){if(a[i]==bi){return 1;}}return -1;} //elsewhere in the code int is_in_a,i,k; int *c;c= (int*)malloc(size_of_c*sizeof(int)) for(i=0,k=0;i <size_of_b;i++){ is_in_a=is_inset (a,size_of_a,b[i]); if (is_in_a==-1){c [k]=b [i];}} I didn't test it but thats approximately what I meant I'm not happy with it I feel like it could be done better I'd be interested to see different (probably better ) ways to do it
14th Jun 2018, 4:19 PM
Rational Agent
+ 1
13th Jun 2018, 6:55 PM
Jai Kumar Garg
+ 1
Fata1 Err0r good but in worst case scenario the complexity will be O(n*m) can you reduce it to O(n+m). â˜șâ˜ș where n = length of a m = length of b
15th Jun 2018, 4:21 AM
Jai Kumar Garg
0
you could make a function that tests if a element of a is in b and if the function return value doesn't indicate a is in b add that element to set c where a-b=c
14th Jun 2018, 3:04 AM
Rational Agent
0
Rational Agent it would be good if you can show
14th Jun 2018, 5:31 AM
Jai Kumar Garg
0
Rational Agent it's not gonna work
14th Jun 2018, 4:51 PM
Jai Kumar Garg