Counting frequencies of Element in an array
Maybe this:

answer = 0
lastVal = -1    //some value not inside [L,R]
count = 0
for i in {l..r}:
  if (array[i] != lastVal)
     count = 1
     lastVal = array[i]
  else
     count++
     if (count == 3)
        count = 0
        answer++


Edit: This code assumes that the set (1,1,1,1) is counted as one triplet not two.
i.e.: (1,1,1) and (1), not (1,1,1) and (1,1,1)