Using PositionIndex
and Merge
,
mergeChron[x_] := Merge[PositionIndex /@ x, Max] // Sort // Keys;
Test:
Case1:
allsublists= {{a1, a5}, {a3, a2, a5}, {a1, a2}};
mergeChron[allsublists]
{a1, a3, a2, a5}
Case2:
alist = RandomSample[Table[ToExpression["a" <> ToString[i]], {i, 1, 1000}]];
range = Range[1000];
Do[len = RandomInteger[{450, 1000}];
select = Sort[RandomSample[range, len]];
sublists[j] = alist[[select]];, {j, 1, 300}]
For num = 300
, all three of the methods produce the same output. However, that's not the case for num = 30
. It might be because of the existence of multiple solutions (as discussed in comments).
num = 300;
allsublists = Array[sublists, num];
f = mergeChron[allsublists];
g = merge[allsublists]; (*Roman's*)
h = topoSort[allsublists]; (*kglr's*)
f == g == h
True