Let's say we have two sets s1
and s2
.
I require three different sets based on these two sets:
- Set of elements that exist in
s1
but not ins2
. - Set of elements that exist in
s2
but not ins1
. - Set of elements that exist in both
s1
ands2
.
These could be easily computed as follows:
s1 = {1, 2, 3, 4, 5}
s2 = {3, 4, 5, 6, 7}
o1 = s1 - s2
o2 = s2 - s1
o3 = s1 & s2
Is there a way to compute these sets more efficiently? I imagine the different set operations have multiple internal processing steps in common, so there might be redundancy.