It's a classical problem from graph theory.
Your pairs are edges and you wanna determine connectivity resp. connected components.
Don't know if the other posters are aware of edge cases (your example data is too simple to show them).
But there are already efficient (and proven) algorithms available!
Cheers Rolf
( addicted to the Perl Programming Language)
In reply to Re: treat pairs, set as one item: looking for better way
by LanX
in thread treat pairs, set as one item: looking for better way
by remiah
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |