Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding intersection and union

Status
Not open for further replies.

timek1m

Programmer
Sep 18, 2001
1
US
my question is about finding union & intersection

if there is data which contains..

u
1 3
2 4

i
3 5
5 6

then program finds out u( union set), and print 1 2 3 4
i(intersection set)and print 5.

i have tried a lot of things and spent a lot of times, but i could not figure it out.
please answer this question if someone know it.
thanks

 
Here's a modified version of the solution I provided for thread271-134331 by Yilei. He/she must be in the same class. If you want the output sorted, you can either insert each element into the h3 array in the correct order or sort the array before printing it.

/^[ui]/ {
union = 0
if ($0 == "u") union = 1
getline
n = split ($0,h1, " ")
split ($0,h3, " ")
getline
split ($0,h2, " ")
j = 0
if (union) j = n
for (k2=1;k2<=n;k2++) {
for (k1=1;k1<=n;k1++) {
if (h1[k1] == h2[k2]) break
}
if (union) {
if (k1 > n) h3[++j] = h2[k2]
}
else {
if (k1 <= n) h3[++j] = h2[k2]
}
}
for (k1=1;k1<=j;k1++) printf(&quot;%d &quot;,h3[k1])
print &quot;&quot;
}

Hope we get an A

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top