Python - Count the duplicate seq element in array
<p>How to solve this</p>

<pre><code>Input: 2
Array = [2,1,3,2,2,2,1,2,2]
Result : 3 (Max of count of seq of 2)
</code></pre>

<p>I just simply used the for loop, it works fine. But is there any other efficient way?</p>

<pre><code>for i in array:
    if i == input:
        Cnt = Cnt + 1
        if Cnt &gt; Result:
            Result = Cnt;
    else:
      Cnt = 0
</code></pre>

<p>What I think your're describing can be solved with <a href="https://en.wikipedia.org/wiki/Run-length_encoding" rel="nofollow">run length encoding.</a></p>

<p>Essentially, you take a sequence of numbers (most often used with characters or simply unsigned bytes) and condense it down into a list of tuples where one value represents the value, and the other represents how many times in a row it occurs.</p>

<pre><code>array = [2,1,3,2,2,2,1,2,2]
runs = []
count = 0

current = array[0] #setup first iteration
for i in array:
    if i == current: #continue sequence
        count += 1
    else:
        runs.append([count, current]) #start new sequence
        current = i
        count = 1
runs.append([count, current]) #complete last iteration

longest_Sequence = max(runs)
</code></pre>

python list