5
$\begingroup$

Here, I am not looking for simply a repeated element, but an indefinitely repeated sequence. For example, if I have the list

{1,3,4,3,4,2,3,3,2,3,3,2,3,3,2,3,3,2,3,3}

I would like Mathematica to output $3$, since the sequence $(2,3,3)$ (with length 3) repeats until the end of the sequence after a certain point. Note, the repeated sequence needs to continue until the end of the list. For example, the sequence $(3,4)$ does repeat itself near the beginning of the list, however I would not want an output of $2$ due to this repetition since the sequence $(3,4)$ does not continue after a certain point.

More formally: If I input a list for which the $i^{th}$ element is $x_i$, I want Mathematica to output the smallest number $n$ for which $x_i=x_{i+n}$ for all sufficiently large $i$.

What function/code would allow me to input a list and then would output the desired result?

$\endgroup$
5
$\begingroup$

Perhaps this?:

Length@Last@FindTransientRepeat[
   {1, 3, 4, 3, 4, 2, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3}, 2]
(*  3  *)

For whether the second argument 2 is sufficient for all your inputs, see FindTransientRepeat.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.