Question: I have some sequences in a string denoted by "@number" (/@\d/)
I want to remove any redundant sequences, where @2 is followed by @2, 
I only want to remove them if another identical @number sequence is found directly after somewhere in the text, so for `@2lorem@2ipsum` the 2nd @2 is removed, but for `@2lorem@1ipsum@2dolor` nothing is removed because @1 is between the two @2 sequences.

```
"@2randomtext@2randomtext@2randomtext@1bla@2bla2@2bla2"

```

becomes:

```
"@2randomtextrandomtextrandomtext@1bla@2bla2bla2

```

Answer: 
```
"@2randomtext@2randomtext@2randomtext@1bla@2bla2@2bla2".gsub /(?<=(@\d))([^@]*)\1/,'\2'
=> "@2randomtextrandomtextrandomtext@1bla@2bla2bla2"

```
easy... split your string into an array, and then compare the number that comes right after that. If it's the same, remove it/them. The complicated (through not that much), is that you can't remove entries from an array while looping through them... so what you need to do is make a recursive function... here's the pseudo:

-= Global values =-  

Decalre StringArray and set it to OriginalString.SplitOn("@")

-= Method RemoveLeadingDuplicates =-  

Declare Counter  

Declare RemoveIndex  

loop for each string in StringArray  

  if previous lead == current lead  

    Set RemoveIndex  

    break from loop  

  else  

    previous lead = current lead  

Increase Counter By 1  

end loop

if RemoveIndex is not null  

  Remove the item at specified index from the array  

  Call RemoveLeadingDuplicates   

Return