The elements of the two arrays are taken as input. The union and intersection are performed on the two given arrays. The union of two arrays will contain all the elements of the two arrays, the common elements will appear only once instead of twice. The intersection of the two arrays will contain the common elements of the two arrays.
INPUT: two Array elements
OUTPUT: the union and intersection of two arrays
PROCESS:
Step 1: [taking the input]
Read m [number of elements of the first array]
For i=0 to m-1 repeat
Read a[i]
[end of ‘for’ loop]
Read n [number of elements of the second array]
For i=0 to n-1 repeat
Read b[i]
[End of ‘for’ loop]
Step 2: [union and intersection of two arrays]
For i=0 to m-1 repeat
Set u[i]<-a[i]
[End of ‘for’ loop]
Set j<-i
For i=0 to n-1 repeat
Set c<-0
For k=0 to j-2 repeat
If b[i]=u[k] then
Set c<-c+1
[End of ‘if’]
[End of ‘for’ loop]
If c=0 then
Set u[j]<-b[i]
Set j<-j+1
[End of ‘if’]
[End of ‘for’ loop]
[displaying the union of the array]
Print "The union of two arrays is: "
For i=0 to j-1 repeat
Print u[i]
[End of ‘for’ loop]
[performing intersection of two arrays]
Set k<-0
For i=0 to m-1 repeat
Set c<-0
For j=0 to n-1 repeat
If a[i]=b[j] then
Set c<-c+1
[End of ‘if’]
[End of ‘for’ loop]
If c≠0 then
Set in[k]<-a[i]
Set k<-k+1
[End of ‘if’]
[End of ‘for’ loop]
[displaying the intersection of the array]
Print "The intersection of two arrays is: "
For i=0 to j-1 repeat
Print in[i]
[End of ‘for’ loop]
Step 3: Stop.
Related