At the moment I've got a set of arrays of elements, say,
[1, 2, 3]
[2, 4]
[5, 6, 7]
[1 , 44]
[5, 12]
etc...
What I want to do is to group these groups into supergroups if they share at least one element together. That is, the arrays above should become:
[1, 2, 3, 4, 44]
[5, 6, 7, 12]
The data I have is much larger and I wonder what is the efficient way of performing such operation.
My guess is take first array, go through all others, if there is intersection, join them and start from the top again, until there is no intersection of first with others. Then follow on to the second one etc...
Is there a better way to do it? I'm especially interested if it could be done easily in PHP, but pseudocode would be good as well..