# Find Union and Intersection of Two Sets C Program

## C Program To Find Union and Intersection of Two Sets

Learn how to perform Set Operations using Functions in C Programming Language. This C program to find union and intersection of Two Arrays with Functions takes two different arrays as input from the user for Set A and Set B. The intersection elements and union elements elements are stored in different arrays.

#### What is a Set?

A set is a collection of distinct objects or elements. It is therefore, a well defined collection of distinct elements. The **union** of set A and B is represented by **A ∪ B**, and it will form a resultant set which consists of all the elements of sets A and B without any repetition. The **intersection** of set A and B is represented by **A ∩ B**, and it forms a resultant set that consists of the common elements from the sets A and B.

This code for calculation intersection and union requires the arrays containing set elements to be in **sorted order**. If they are not in sorted order, then there will be a problem in fetching the union of the two sets.

#### Example of Union and Intersection of Two Arrays

A = {1, 2, 3, 4, 5, 6}

B = {4, 5, 6, 7, 8, 9}

Union: (**A ∪ B**) = {1, 2, 3, 4, 5, 6, 7, 8, 9}

Intersection: (**A ∩ B) **= {4, 5, 6}

**Must Read: C Program To Find Value of Arithmetic Expression**

#### C Program For Set Operations using Functions – Union and Intersection Methods

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | #include <stdio.h> #define LIMIT 6 void get_set_elements(int array[]) { int count; for(count = 0; count < LIMIT; count++) { printf("Enter Element No. %d:\t", count + 1); scanf("%d", &array[count]); } } void display_input_set(int array[], int total) { int count; for(count = 0; count < total; count++) { printf("%3d", array[count]); } } void sort_array_elements(int array[]) { int i, j, temp; for(i = 0; i < LIMIT; i++) { for(j = i + 1; j < LIMIT; j++) { if(array[i] > array[j]) { temp = array[j]; array[j] = array[i]; array[i] = temp; } } } } int intersection_function(int set_A[], int set_B[], int intersection_array[]) { int i = 0, j = 0, k = 0; while((i < LIMIT) && (j < LIMIT)) { if(set_A[i] < set_B[j]) { i++; } else if(set_A[i] > set_B[j]) { j++; } else { intersection_array[k] = set_A[i]; i++; j++; k++; } } return(k); } int union_function(int set_A[], int set_B[], int union_array[]) { int i = 0, j = 0, k = 0; while((i < LIMIT) && (j < LIMIT)) { if(set_A[i] < set_B[j]) { union_array[k] = set_A[i]; i++; k++; } else if(set_A[i] > set_B[j]) { union_array[k] = set_B[j]; j++; k++; } else { union_array[k] = set_A[i]; i++; j++; k++; } } if(i == LIMIT) { while(j < LIMIT) { union_array[k] = set_B[j]; j++; k++; } } else { while(i < LIMIT) { union_array[k] = set_A[i]; i++; k++; } } return(k); } int main() { int set_A[LIMIT], set_B[LIMIT], intersection_array[LIMIT], union_array[LIMIT*2]; int total; printf("\nEnter Elements of SET A\n"); get_set_elements(set_A); printf("\nEnter Elements of SET B\n"); get_set_elements(set_B); printf("\nElements of Set A:\t"); display_input_set(set_A, LIMIT); printf("\nElements of Set B:\t"); display_input_set(set_B, LIMIT); sort_array_elements(set_A); sort_array_elements(set_B); printf("\nSorted Set A:\t"); display_input_set(set_A, LIMIT); printf("\nSorted Set B:\t"); display_input_set(set_B, LIMIT); total = intersection_function(set_A, set_B, intersection_array); printf("\nIntersection Set:\t"); display_input_set(intersection_array, total); total = union_function(set_A, set_B, union_array); printf("\nUnion Set:\t"); display_input_set(union_array, total); printf("\n"); return 0; } |

#### Output

If you get any compilation errors or have any doubts in this C program to find union and intersection of two Sets, let us know about it in the comment section below.

Why is it important to sort the array to find union of two sets in C?

Amazing code union and intersection of two array in C programming.