Why part is explained nicely in @ciao's answer. This post deals with the part:
I want to detect whether this list has repeated element a,a or b,b
You can get what expectyou expected to get from Cases[...]
using the new-in-v-10 function SequenceCases
:
list={a,b,a,b,b,b,a,b,a,a,b};
SequenceCases[list,{Repeated[x_,{2}]}]
{{b, b}, {a, a}}
Or, define a Boolean
function that returns True
when the input list contains repeated elements:
containsRepeatedQ = SequenceCases[#,{Repeated[x_,{#2}]}] != {}&;
containsRepeatedQ[{a,b,a,b,a,b,a,b}, 2]
False
containsRepeatedQ[{a,b,a,b,a,b,a,a,b}, 2]
True