Spreadsheet: 802110d1666657428-2d-spill-array-gk-test
Sheet: vertical

united kingdom | glasgow
germany | berlin
turkey | istanbul |  |  |  |  |  |  |  | =TEXTSPLIT(TEXTJOIN("|",,MAP(D5#,LAMBDA(x,TEXTJOIN(",",,UNIQUE(IF($A$2:$A$26=x,IF($B$2:$B$26="","",$B$2:$B$26),"")))))),",","|",,,"")
france | paris
france | lyon |  | belgium |  |  |  |  |  | brussels
belgium | brussels |  | czechia |  |  |  |  |  | prague
united kingdom | london |  | france |  | paris | strasbourg |  |  | paris | lyon | strasbourg
italy | rome |  | germany |  | frankfurt | hamburg |  |  | berlin | hamburg | frankfurt
hungary | budapest |  | greece |  |  |  |  |  | athens
hungary |  |  | hungary |  | budapest |  |  |  | budapest
spain | madrid |  | italy |  | rome |  |  |  | rome | napoli
portugal | lisbon |  | portugal |  |  |  |  |  | lisbon
spain | bilbao |  | russia |  |  |  |  |  | moscow
czechia | prague |  | spain |  | madrid |  |  |  | madrid | bilbao
russia | moscow |  | turkey |  |  |  |  |  | istanbul
germany | hamburg |  | united kingdom |  | glasgow | london | manchester |  | glasgow | london | cardiff | manchester
united kingdom | cardiff
[NAME] | vienna |  | =LET(a,SORT(UNIQUE(FILTER(A2:B26,(A2:A26<>"")*(B2:B26<>""))),{1,2}),b,INDEX(a,,1),c,INDEX(a,,2),d,UNIQUE(b),HSTACK(d,TEXTSPLIT(TEXTJOIN("|",,BYROW(d,LAMBDA(x,TEXTJOIN(",",,IF(b=x,c,""))))),",","|",,,"")))
italy | napoli |  |  | vienna
france | paris |  | belgium | brussels
greece | athens |  | czechia | prague
united kingdom | manchester |  | france | lyon | paris | strasbourg
italy | napoli |  | germany | berlin | frankfurt | hamburg
germany | frankfurt |  | greece | athens
france | strasbourg |  | hungary | budapest
 |  |  | italy | napoli | rome
 |  |  | portugal | lisbon
 |  |  | russia | moscow
 |  |  | spain | bilbao | madrid
 |  |  | turkey | istanbul
 |  |  | united kingdom | cardiff | glasgow | london | manchester
 |  |  | =LET(a,SORT(UNIQUE(FILTER(A2:B26,(A2:A26<>"")*(B2:B26<>""))),{1,2}),b,INDEX(a,,1),c,INDEX(a,,2),d,UNIQUE(b),HSTACK(d,IFNA(DROP(REDUCE("",d,LAMBDA(x,y,VSTACK(x,TRANSPOSE(FILTER(c,b=y))))),1),"")))
 |  |  |  | vienna
 |  |  | belgium | brussels
 |  |  | czechia | prague
 |  |  | france | lyon | paris | strasbourg
 |  |  | germany | berlin | frankfurt | hamburg
 |  |  | greece | athens
 |  |  | hungary | budapest
 |  |  | italy | napoli | rome
 |  |  | portugal | lisbon
 |  |  | russia | moscow
 |  |  | spain | bilbao | madrid
 |  |  | turkey | istanbul
 |  |  | united kingdom | cardiff | glasgow | london | manchester
 |  |  | =LET(a,SORT(UNIQUE(FILTER(A2:B26,(A2:A26<>"")*(B2:B26<>""))),{1,2}),b,INDEX(a,,1),c,INDEX(a,,2),d,UNIQUE(b),VSTACK(d,TRANSPOSE(IFNA(DROP(REDUCE("",d,LAMBDA(x,y,VSTACK(x,FILTER(c,b=y)))),,1),""))))
 |  |  |  | vienna
 |  |  | belgium | brussels
 |  |  | czechia | prague
 |  |  | france | lyon | paris | strasbourg
 |  |  | germany | berlin | frankfurt | hamburg
 |  |  | greece | athens
 |  |  | hungary | budapest
 |  |  | italy | napoli | rome
 |  |  | portugal | lisbon
 |  |  | russia | moscow
 |  |  | spain | bilbao | madrid
 |  |  | turkey | istanbul
 |  |  | united kingdom | cardiff | glasgow | london | manchester
 |  |  | =LET(a,SORT(UNIQUE(FILTER(A2:B26,(A2:A26<>"")*(B2:B26<>""))),{1,2}),b,INDEX(a,,1),c,INDEX(a,,2),d,[NAME],MATCH(b,b,0),MAKEARRAY(ROWS(d),1+MAX(FREQUENCY(e,[NAME],y,IF(y=1,INDEX(d,x,1),IFERROR(INDEX(FILTER(c,b=INDEX(d,x,1)),y-1,1),"")))))
 |  |  |  | vienna
 |  |  | belgium | brussels
 |  |  | czechia | prague
 |  |  | france | lyon | paris | strasbourg
 |  |  | germany | berlin | frankfurt | hamburg
 |  |  | greece | athens
 |  |  | hungary | budapest
 |  |  | italy | napoli | rome
 |  |  | portugal | lisbon
 |  |  | russia | moscow
 |  |  | spain | bilbao | madrid
 |  |  | turkey | istanbul
 |  |  | united kingdom | cardiff | glasgow | london | manchester