Set Operations
Implementation of set operations like union, intersection, difference and set membership.
Union Operation :
void unionOfArray(int a[], int b[], int c[], int n1, int n2) {
// Index pointers
int i = 0, j = 0, k= 0;
while (i < n1 && j < n2) {
// If elements are same, copy once and increment all
if (a[i] == b[j]) {
c[k] = a[i];
i++;
j++;
} else if (a[i] < b[j]) {
c[k] = a[i];
i++;
} else {
c[k] = b[j];
j++;
}
k++;
}
// Copy remaining elements from first array
while (i < n1) {
c[k] = a[i];
i++;
k++;
}
// Copy remaining elements from second array
while (j < n2) {
c[k] = b[j];
j++;
k++;
}
}Intersection Operation :
Difference Operation :
Set Membership :
Last updated