>>911 Squeak Smalltalk

| input groups |
input := 'goose pigeon
cat dog
eel goldfish
goose duck
horse dog
cod eel
dove pigeon
dog rhino
goldfish squid
goose lark'.

groups := OrderedCollection new.
input linesDo: [:line |
 | pair |
 pair := line subStrings.
 (groups detect: [:group | group includesAnyOf: pair] ifNone: [groups add: Set new])
  addAll: pair
].
(groups collect: [:each | each asSortedCollection joinSeparatedBy: ' ']) asStringWithCr

"=> 'dove duck goose lark pigeon
cat dog horse rhino
cod eel goldfish squid' "