Create a list of unique numbers by applying transitive closure
问题 I have a list of tuples (each tuple consists of 2 numbers) like: array = [(1, 2), (1, 3), (2, 4), (5, 8), (8, 10)] Lets say, these numbers are ids of some db objects (records) and inside a tuple, there are ids of duplicate objects. Which means 1 and 2 are duplicate. 1 and 3 are duplicate which means 2 and 3 are also duplicate. if a == b and b == c then a == c Now I want to merge all these duplicate objects ids into a single tuple like this: output = [(1, 2, 3, 4), (5, 8, 10)] I know I can do