Determine repeating number of a given number and corresponding position

If you have Mma version >= 10.1 , this does the job :

data = {1, 1, 1, -1, -1, 1, 1, -1};
{First[#], Last[#] - First[#] + 1} & /@ SequencePosition[data, {Repeated[1]}, Overlaps -> False]

In terms of speed, this solution is catastrophic. See studies in other answers.


I think this question may still be considered a duplicate of Finding negative sequences in a large list: optimization but it seems to permit a somewhat simpler solution which I would like to post.

This is still written for performance over brevity. I shall benchmark it later if I remember.

fn[a_List, n_] :=
  Module[{p, q, r},
    p = Pick[Range @ Length @ a, Unitize[n - a], 1];
    q = Prepend[p + 1, 1];
    r = Append[p, Length@a + 1] - q;
    Pick[{q, r}\[Transpose], Unitize @ r, 1]
  ]

Example:

data = {1, 1, 1, -1, -1, 1, 1, -1};

fn[data, 1]
{{1, 3}, {6, 2}}

{#1, Length[{##}]} & @@@ Split[PositionIndex[data][1], #2 - #1 == 1 &]
{#1, Length[{##}]} & @@@ Split[Pick[Range@Length@data, 1 - Unitize[1 - data], 1], #2 - #1 == 1 &]