Detriplicate a string
Jelly, 6 bytes
Qx2œ&@
Try it online! or verify all test cases.
How it works
Qx2œ&@ Main link. Argument: s (string)
Q Unique; deduplicate s.
x2 Repeat each character.
œ&@ Take the multiset intersection of s and the previous result.
JavaScript (ES6), 42 48
Edit A whopping 6 bytes saved thx @Neil
s=>s.replace(k=/./g,c=>(k[c]+=c)[11]?'':c)
Explanation: I use the properties 'a'...'z' of object k
to store info for each character (object k is a regexp in this case just to save bytes).
These properties are initially undefined
. In javascript adding a number to undefined
gives NaN
(quite sensible), but adding a string 'X' gives "undefinedX"
- a string of length 10 (silly). Adding more characters you get longer strings. If the obtained string for a given character is longer than 11, that character is not copied to output.
Test
F=
s=>s.replace(k=/./g,c=>(k[c]+=c)[11]?'':c)
test=`
xxxxx
xx
abcabc
abcabc
abcdabcaba
abcdabc
abacbadcba
abacbdc
aaabcbccdbabdcd
aabcbcdd`.split`\n`
for(i=0;i<test.length;)
a=test[i++],b=test[i++],r=F(a),
console.log(r==b?'OK':'KO',a,'->',r,b)
Python 2, 48 bytes
lambda s:reduce(lambda r,c:r+c*(r.count(c)<2),s)
c[r.count(c)/2:]
is a same-length alternative to c*(r.count(c)<2)
.
49 bytes:
r=''
for c in input():r+=c*(r.count(c)<2)
print r