Another method using GroupBy
:
Values[GroupBy[Catenate[{a, b}], First, If[Length[#] == 2, Sequence @@@ {#[[1, All]], #[[2]][[2;;]]}, #[[1]]] &]](*{{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}*)
Another method using GroupBy
:
Values[GroupBy[Catenate[{a, b}], First, If[Length[#] == 2, Sequence @@@ {#[[1, All]], #[[2]][[2;;]]}, #[[1]]] &]](*{{1, 3, 5, 2}, {2, 6, 2, 1}, {3, 5, 6, 1, 2}, {4, 2, 3}}*)