The union of two sets, A
and B
, is the set of elements in A
,B
, or in both A
and B
.
Set
has a property in which all the elements in the set are unique.
A = {3,5,7 8,4,9}
B = {1,2,3,5,9,7,8,6}
A ∩ B = { 3, 5, 9, 7, 8 }
#include <iostream>using namespace std;#define sizeOfA 6#define sizeOfB 8int findCommonElement(int setA[], int sizeOfSetA, int setB[], int sizeOfSetB){int commonElemnt =0;for(int i=0; i<sizeOfSetA; i++){for(int j=0; j<sizeOfSetB; j++){if(setA[i]==setB[j]){commonElemnt++;}}}}int *findIntersection(int setA[], int sizeOfSetA, int setB[], int sizeOfSetB, int commonElement){int *IntersectionArray;// find size of new Arrayint sizeOfIntersectionArray= commonElement;IntersectionArray= new int[sizeOfIntersectionArray];int k=0; // to insert elemnt in the union array we use this iteratorfor(int i=0; i<sizeOfSetB; i++){for(int j=0; j<sizeOfSetA; j++){if (setA[j]==setB[i]){IntersectionArray[k]=setA[j];k++;}}}return IntersectionArray;}int main(){int * IntersectionArray;// declare two array A and Bint A[sizeOfA] = {3,5,7,8,4,9};int B[sizeOfB] = {1,2,3,5,9,7,8,6};int commonElement=findCommonElement(A,sizeOfA, B,sizeOfB);// get number of Common Element of both arraysint sizeOfNewArray=commonElement-1;// calculate size ofnew ArrayIntersectionArray=findIntersection(A,sizeOfA,B,sizeOfB, commonElement);//Print the array elementcout<<"{ ";for(int i =0; i<sizeOfNewArray; i++){if(i==sizeOfNewArray-1){cout<<IntersectionArray[i]<<" }";}else{cout<<IntersectionArray[i]<<", ";}}return 0;}
setA
and setB
.findCommonElement
that can find the count of the common elements between two sets.findIntersection
that can find the intersection of two set setA
and setB
.setA
and setB
.findIntersection
and save the array return by function in the IntersectionArray
pointer variable.setA
and setB
.Free Resources