I'm looking for a clever/fast C++ algorithm, which would allow me to do grouping of several lists of objects when they contain common objects inside. Let's say I have N lists, each containing 1..M objects (O) associated with one element E:
[O1, O2] -> E1
[O3] -> E2
[O1, O4, O5] -> E3
[O2, O5] -> E4
[O3, O6] -> E5
I wish to rearrange them into the following:
[O1, O2, O4, O5] -> [E1, E3, E4]
[O3, O6] -> [E2, E5]
The result has all common objects grouped together with all associated elements. No object is shared in the end between lists.