Remove elements that appear m times
I don't really understand your problem, so here is how you can solve a similar problem: instead of removing duplicated, we want each element to appear at most twice. In other words, if an element appears in a run of length $\ell$, we replace it with a run of length $\min(\ell,2)$. Suppose that the array is $A_1,\ldots,A_n$.


Assign $\mathrm{curr} \gets A_1$. This variable will store the value being repeated in the current run.
Assign $\ell \gets 1$. This is the length of the current run.
Assign $\nu \gets 1$. This is the length of the "new" array.
Loop over the values $2 \leq i \leq n$:


If $A_i = \mathrm{curr}$, increase $\ell$ by 1.
If $A_i \neq \mathrm{curr}$, let $\mathrm{curr} \gets A_i$, and set $\ell \gets 1$.
If $\ell \leq 2$, insert the new element: increase $\nu$ by 1, and assign $A_\nu \gets \mathrm{curr}$.

Return $A_1,\ldots,A_\nu$.


You can prove by induction that $\nu \leq i$, and so information is erased only after it is read.

A more challenging task is: "If an element appears in a run of length $\ell \geq m$, replace it with a run of length $2$". In other words, we replace a run of length $\ell$ with a run of length $2$ whenever $\ell \geq m$ (assuming $m \geq 2$). We can accomplish this using a similar strategy:


Assign $\mathrm{curr} \gets A_1$. This variable will store the value being repeated in the current run.
Assign $\ell \gets 1$. This is the length of the current run.
Assign $\nu \gets 1$. This is the length of the "new" array.
Loop over the values $2 \leq i \leq n$:


If $A_i = \mathrm{curr}$, increase $\ell$ by 1.
If $A_i \neq \mathrm{curr}$, let $\mathrm{curr} \gets A_i$, and set $\ell \gets 1$.
If $\ell &lt; m$, insert the new element: increase $\nu$ by 1, and assign $A_\nu \gets \mathrm{curr}$.
If $\ell = m$, remove $m-2$ occurrences of $\mathrm{curr}$: decrease $\nu$ by $m-2$.

Return $A_1,\ldots,A_\nu$.