I am a bit new to both Mathematica and StackExchange, so I apologize if I have made any mistakes in my question or missed an obvious answer. I have lists of the form
{{1, 2, 3, 4}, {1, 4, 5, 8}, {7, 8, 11, 12}}
and want to return lists where the union of any two adjoining elements is taken if they share two elements in common. For the example above, this would result in the list below:
{{1, 2, 3, 4, 5, 8},{7, 8, 11, 12}}
I have used a Table construction (below) to find the Length of each of the intersections of adjoining elements, but am having trouble combining the lists when they share exactly two elements.
Length /@ Table[Intersection[#[[i]], #[[i + 1]]], {i, Length[#] - 1}] & @ {{1, 2, 3, 4}, {1, 4, 5, 8}, {7, 8, 11, 12}}
I have done a lot of brainstorming about this, but every way I think of runs into issues when considering the special cases of multiple of these intersections chained together (which may be able to be solved by recursion) such as:
{{1,2,3,4},{1,4,5,6},{5,6,7,8},{10,11,12,13}} -> {{1,2,3,4,5,6,7,8},{10,11,12,13}}
or when two of the unions have to occur at different points in the list such as:
{{1,2,3,4},{1,4,5,6},{7,8,9,10},{11,12,13,14},{13,14,15,16}} -> {{1,2,3,4,5,6},{7,8,9,10},{11,12,13,14,15,16}}
Furthermore the number of lists will be of arbitrary length. Thank you for any help!