[Question]
  [
Having spend some time on this site I have come to enjoy things being as short as possible. That may be the reason why I'm recently kind of offended by strings containing the same characters more than once. Your job is to write a function or program which *condenses* a given string according to the following rules:
* Start with a *0-condensation*, that is look for the first (leftmost) pair of the same characters with 0 other characters between them. If such a pair is found, remove one of the two characters and restart the algorithm by performing another *0-condensation*. If no such pair is found, proceed with the next step. Examples:  
`programming` -C0-> `programing`  
`aabbcc` -C0-> `abbcc`  
`test` -C0-> `test`
* Then perform a *1-condensation*, that is look for the first pair of same characters with 1 other character between them. If such a pair is found remove one of them **and all characters between them** and restart with a *0-condensation*. If no such pair is found, proceed with the next step. Examples:  
`abacac` -C1-> `acac`  
`java` -C1-> `ja`
* Continue with a *2-condensation* and so on up to a *n-condensation* with *n* being the length of the original string, each time restarting after a condensation removed some letters. Examples:  
`programing` -C2-> `praming`  
`abcdafg` -C3-> `afg`
The resulting string is called *condensed* and contains each character at most once.
---
**Input:** 
A lower case string of printable ascii-characters.
**Output:** 
The *condensed* string according to the rules above.
**Examples:**
```
examples     -> es
programming  -> praming
puzzles      -> puzles
codegolf     -> colf
andromeda    -> a
abcbaccbabcb -> acb
if(x==1):x++ -> if(x+
fnabnfun     -> fun
abcdefae     -> abcde
```
Detailed examples to clarify how the algorithm works:
```
fnabnfun -C0-> fnabnfun -C1-> fnabnfun -C2-> fnfun -C0-> fnfun -C1-> fun -C0-> fun 
 -C1-> fun -C2-> ... -C8-> fun
abcbaccbabcb -C0-> abcbacbabcb -C0-> abcbacbabcb -C1-> abacbabcb -C0-> abacbabcb 
 -C1-> acbabcb -C0-> acbabcb -C1-> acbcb -C0-> acbcb -C1-> acb -C0-> acb 
 -C1-> ... -C12-> acb
```
Your approach doesn't have to implement the algorithm from above as long as your solution and the algorithm return the same output for all allowed inputs.
This is a [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") challenge.
---
Thanks to [@Linus](http://meta.codegolf.stackexchange.com/users/46756/linus) for helpful sandbox comments!
  
[Answer]
## JavaScript (ES6), 74 bytes
```
f=
(s,n=0,m=s.match(`(.).{${n}}\\1`))=>s[n]?m?f(s.replace(...m)):f(s,n+1):s
;
```
```
<input oninput=o.textContent=f(this.value)><pre id=o>
```
[Answer]
# Perl, ~~38~~ ~~31~~ ~~30~~ 29 bytes
This should should leave the non-golfing languages far behind...
-1 for `$-[0]` thanks to [Riley](https://codegolf.stackexchange.com/users/57100/riley)
-1 for `@{-}` thanks to [Dada](https://codegolf.stackexchange.com/users/55508/dada)
Includes +1 for `-p`
Give input on STDIN
`condense.pl`:
```
#!/usr/bin/perl -p
s/(.)\K.{@{-}}\1// while/./g
```
This 27 byte version should work but it doesn't because perl doesn't interpolate `@-` in a regex (see <https://stackoverflow.com/questions/39521060/why-are-etc-not-interpolated-in-strings>)
```
#!/usr/bin/perl -p
s/(.)\K.{@-}\1// while/./g
```
[Answer]
# [CJam](http://sourceforge.net/projects/cjam/), 35 bytes
```
rL{_,{{(_@_@#I={I)>]sj}*}h]s}fI}j
```
[Try it online!](http://cjam.tryitonline.net/#code=ckx7Xyx7eyhfQF9AI0k9e0kpPl1zan0qfWhdc31mSX1qCgo&input=Zm5hYm5mdW4)
---
```
rL{                            }j   | run recursion on input
   _,{                      }fI     | for I from 0 to length(input)
      {                 }h]s        | one pass & clean up
       (_@                          | slice and store leading element A
          _@#I={      }*            | if next A is I steps away
                I)>                 | slice off I+1 element
                   ]sj              | clean up & recursion
```
You can see the individual condensations by [inserting `ed`](http://cjam.tryitonline.net/#code=ckx7ZWRfLHt7KF9AX0AjST17SSk-XXNqfSp9aF1zfWZJfWoKCg&input=Zm5hYm5mdW4)
[Answer]
# Python 2, ~~117~~ ~~104~~ 101 bytes
Recursively do the necessary replacements. I build the regex dynamically.
```
import re
def f(s,i=0):t=re.sub(r"(.)%s\1"%("."*i),r"\1",s);e=s==t;return i>len(t)and t or f(t,i*e+e)
```
[**Try it online**](https://repl.it/DiEM/2)
[Answer]
# Perl 53 52
Includes +1 for -p
```
for($i=0;$i<length;){$i=(s/(.).{$i}\1/\1/)?0:$i+1;}
```
Try it on [ideone](http://ideone.com/WyyE3d).
[Answer]
# Mathematica, 101 bytes
```
NestWhile[i=0;StringReplace[#,a_~~_~RepeatedNull~i++~~a_:>a,1]&,#,SameQ,2,ByteCount@#]&~FixedPoint~#&
```
There should be a way to make this shorter...
[Answer]
# PHP, 90 Bytes
```
for($s=$argv[$c=1];$s[$i=++$i*!$c];)$s=preg_replace("#(.).{{$i}}\\1#","$1",$s,1,$c);echo$s;
```
or 92 Bytes 
```
for($s=$argv[1];$s[$i];$i=++$i*!$c)$s=preg_replace("#(.).{".+$i."}\\1#","$1",$s,1,$c);echo$s;   
```
[Answer]
# Ruby, ~~75~~ ~~64~~ 57 bytes
(56 bytes of code + `p` command line option.)
Using string interpolation inside a regex to control the length of the matches that are replaced.
```
i=0
~/(.).{#{i}}\1/?sub($&,$1)&&i=0: i+=1while i<$_.size
```
Test:
```
$ ruby -p condense.rb <<< fnabnfun
fun
```
[Answer]
# [Haskell](https://www.haskell.org/), ~~97~~ 88 bytes
```
(0?)
(a:s)!(b:t)|a==b=a:t|1<3=a:s!t
s!_=s
m?s|length s<m=s|a<-s!drop m s=sum[m+1|a==s]?a
```
[Try it online!](https://tio.run/##bc7NaoQwEADgc32KcelBEaHSmxh8gd56bEuZxMSV5g8ngiw@e23M3twewsxkvszkivQjtd4VsM@9eOnLrMCWyrzgbSg3ZIwzbMPWdK8xUh4yyr8ZZaanTUs7hitQZxht2NWUD7PzYIAYLebDVM3xnr563A1OFhgMLgO/hPcwv1l4BgUXuaLxWtLlqa5B0rntZzfOaMxkxyR8LGL@wJbbLQ1JZjnyMxFukKPTKo0RMTkDtPH3Rg6YBD60ueAo4onxLgQ/m0kVK2NN2a5VlcxxUZ2VssitWmwSMf6zaZAK5X3LUey/Qmkcaa@F938 "Haskell – Try It Online")
---
### Old 97 byte bersion:
```
(a:s)!(b:t)|a==b=a:t|1<3=a:s!t
s!_=s
m?s|length s==m=s|a<-s!drop m s=(last$0:[m+1|a==s])?a
c=(0?)
```
[Try it on ideone](https://ideone.com/NkXY2C).
**Explanation:**
```
(a:s)!(b:t)|a==b = a:t         --perform condensation
           |1<3  = a:s!t       --recursively compare further
 s   ! _         = s           --no condensation performed
```
The `(!)` function performs one n-condensation when given a string once whole and once with the first n characters removed, e.g. `abcdbe` and `cdbe` for a 2-condensation, by recursively comparing the two leading characters.
```
m?s|length s==m   = s         --stop before performing length-s-condensation
   |a <- s!drop m s           --a is the m-condensation of s
    = (last$0:[m+1|a==s])?a   --disguised conditional:
                              -- if a==s       if the m-condensation did not change s
                              -- then (m+1)?a  then perform m+1-condensation
                              -- else 0?a      else restart with a 0-condensation
c=(0?)                        -- main function, initialise m with 0
```
]