Imagine I have a complicated sequence {....,a1,a2,a3,...,x1,x2,x1,x2,x1,x2,x1,x2,....,z1,z2,z3,...}
I want to replace the repeating pattern x1,x2,x1,x2,x1,x2,x1,x2 (which could be anything more than two - here it is four repetitions) by just x1,x2 or separately by x1,x2,x1,x2.
I tried SequenceReplace but was not successful.
SequenceReplace[{Repeated[PatternSequence[x1, x2], {2, Infinity}],
e_} -> Union[{ x1, x2 }, e ]]@{x1, x2, x1, x2, x1, x2, x3, x4}
did not work..
SequenceReplace[{x1, x2, x1, x2, x1, x2, x3, x4}, {PatternSequence[x1, x2] .. } -> Splice[{x1, x2}]]
suit your needs? $\endgroup$somelist//.{h___,x1,x2,x1,x2,t___}->{h,x1,x2,t}
$\endgroup$