| Bytes | Lang | Time | Link |
|---|---|---|---|
| 009 | Uiua | 250914T190908Z | nyxbird |
| 033 | jq Rr | 250914T140301Z | pmf |
| 124 | C++ | 250914T131656Z | Toby Spe |
| 139 | C++ | 190226T013319Z | zelcon |
| 012 | APLNARS | 250914T083508Z | Rosario |
| 082 | JavaScript Node.js | 190225T211822Z | Kamil Na |
| 034 | JavaScript | 190225T213233Z | Shaggy |
| 070 | R | 190225T204641Z | Sumner18 |
| 005 | Japt | 190225T171911Z | Quintec |
| 008 | Brachylog v2 | 190225T190208Z | ais523 |
| 045 | JavaScript | 190225T174102Z | kamoroso |
| 008 | APL Dyalog Extended | 190225T123750Z | Adá |
| 059 | PowerShell | 190225T103215Z | mazzy |
| 090 | Java 8 | 180316T131307Z | Kevin Cr |
| 128 | sed and coreutils | 121016T005007Z | Thor |
| nan | 121011T012118Z | beary605 | |
| 070 | PHP | 121011T181854Z | hengky m |
| 241 | Perl | 121011T132101Z | Gareth |
| 133 | Ocaml | 121011T094258Z | ReyCharl |
| 018 | K | 121011T191335Z | tmartin |
| 078 | PHP | 121011T182048Z | milo5b |
| 137 | PHP | 121011T164417Z | W Kristi |
| 077 | C# – | 121011T150304Z | Mormegil |
| 036 | Ruby | 121011T031951Z | Steven R |
| 012 | J | 121011T135055Z | Gareth |
| 013 | GolfScript | 121011T061434Z | Howard |
| 014 | GolfScript | 121011T091655Z | Peter Ta |
| 055 | Perl | 121011T050400Z | Daniel H |
| 063 | Mathematica | 121011T003440Z | DavidC |
| 044 | Perl | 121011T034453Z | flodel |
| 056 | Python | 121011T013405Z | arshajii |
Uiua, 9 bytes
▽±+⧆⟜⊸⍜⇌⧆
▽±+⧆⟜⊸⍜⇌⧆
⍜⇌⧆ # get the number of repeats following each element
⧆⟜ # get the number of repeats preceding each element
+ # add them
▽± ⊸ # keep elements with at least 1
jq -Rr, v1.5+, 33 bytes
Another noncompetitive (non-TIOBE 20) entry:
./""|.-(.-[group_by(.)[][1]])|add
C++, 124 bytes
#include <algorithm>
void f(auto&s){auto a=begin(s),z=end(s);s.erase(remove_if(a,z,[=](char
c){return count(a,z,c)<2;}),z);}
Just uses the erase-remove idiom, inefficiently using std::count() to determine how many times each letter appears.
This code assumes that <algorithm> causes std::begin and std::end to be defined (otherwise we also need <iterator>) and that std::string iterators are not raw pointers (so that argument-dependent lookup finds std-namespace functions).
Demo
#include <iostream>
#include <string>
int main(int, char **argv)
{
while (*++argv) {
std::string s{*argv};
f(s);
std::cout << s << '\n';
}
}
C++, 139 bytes
string s;cin>>s;string w{s}; auto l=remove_if(begin(s),end(s),[&w](auto&s){return count(begin(w),end(w),s)==1;});s.erase(l,end(s));cout<<s;
ungolfed:
#include <algorithm>
#include <string>
#include <iostream>
int main() {
using namespace std;
string s;
cin >> s;
const string w{s};
auto l = remove_if(begin(s), end(s), [&w](auto& s) {
return count(begin(w), end(w), s) == 1;
});
s.erase(l, end(s));
cout << s;
return 0;
}
APL(NARS), 12 chars
{⍵/⍨⍵∊⍵∼⍦∪⍵}
Find the elements that are in the list repeated (that are ⍵∼⍦∪⍵), sign them in a boolean array, for doing a new list composed only from them. It could be okay for all other list too even if not chars composite.
test:
{⍵/⍨⍵∊⍵∼⍦∪⍵}'helloworld'
llool
JavaScript (Node.js), 82 bytes
p=>[...p].map((v,i,a)=>a.filter(f=>f==v).length).reduce((a,c,i)=>c>1?a+=p[i]:a,[])
JavaScript, 34 bytes
Input as a string, output as a character array.
s=>[...s].filter(x=>s.split(x)[2])
R, 70 bytes
a=utf8ToInt(scan(,''));intToUtf8(a[!a%in%names(table(a)[table(a)<2])])
A poor attempt, even from a TIOBE top 20 language. I know something can be done about the second half, but at the moment, any golfs escape me.
Brachylog (v2), 8 bytes
⊇.oḅlⁿ1∧
Function submission. Technically noncompeting because the question has a limitation on what langauges are allowed to compete (however, several other answers have already ignored the restriction).
Explanation
⊇.oḅlⁿ1∧
⊇ Find {the longest possible} subset of the input
o {for which after} sorting it,
ḅ and dividing the sorted input into blocks of identical elements,
lⁿ1 the length of a resulting block is never 1
. ∧ Output the subset in question.
JavaScript, 45 bytes
s=>[...s].filter(c=>s.match(c+'.*'+c)).join``
APL (Dyalog Extended), 8 bytesSBCS
Anonymous tacit prefix function.
∊⊢⊆⍨1<⍧⍨
⍧⍨ count-in selfie (count occurrences of argument elements in the argument itself)
1< Boolean mask where one is less than that
⊢⊆⍨ partition the argument by that mask (beginning a new partition on 1s and removing on 0s)
∊ ϵnlist (flatten)
PowerShell, 59 bytes
"$args"-replace"[^$($args|% t*y|group|?{$_.Count-1}|% n*)]"
Less golfed:
$repeatedСhars=$args|% toCharArray|group|?{$_.Count-1}|% name
"$args"-replace"[^$repeatedСhars]"
Note: $repeatedChars is an array. By default, a Powershell joins array elements by space char while convert the array to string. So, the regexp contains spaces (In this example, [^l o]). Spaces do not affect the result because the input string contains letters only.
Java 8, 90 bytes
s->{for(char c=96;++c<123;s=s.matches(".*"+c+".*"+c+".*")?s:s.replace(c+"",""));return s;}
Explanation:
s->{ // Method with String as both parameter and return-type
for(char c=96;++c<123; // Loop over the lowercase alphabet
s=s.matches(".*"+c+".*"+c+".*")?
// If the String contains the character more than once
s // Keep the String as is
: // Else (only contains it once):
s.replace(c+"","")); // Remove this character from the String
return s;} // Return the modified String
sed and coreutils (128)
Granted this is not part of the TIOBE list, but it's fun (-:
<<<$s sed 's/./&\n/g'|head -c -1|sort|uniq -c|sed -n 's/^ *1 (.*)/\1/p'|tr -d '\n'|sed 's:^:s/[:; s:$:]//g\n:'|sed -f - <(<<<$s)
De-golfed version:
s=helloworld
<<< $s sed 's/./&\n/g' \
| head -c -1 \
| sort \
| uniq -c \
| sed -n 's/^ *1 (.*)/\1/p' \
| tr -d '\n' \
| sed 's:^:s/[:; s:$:]//g\n:' \
| sed -f - <(<<< $s)
Explanation
The first sed converts input into one character per line. The second sed finds characters that only occur once. Third sed writes a sed script that deletes unique characters. The last sed executes the generated script.
Python 2.7 (52 51), Python 3 (52)
I didn't expect it to be so short.
2.7: a=raw_input();print filter(lambda x:a.count(x)>1,a)
3.0: a=input();print''.join(i for i in a if a.count(x)>1)
raw_input(): store input as a string (input() = eval(raw_input()))
(Python 3.0: input() has been turned into raw_input())
filter(lambda x:a.count(x)>1,a):
Filter through all characters within a if they are found in a more than once (a.count(x)>1).
PHP - 70
while($x<strlen($s)){$c=$s[$x];echo substr_count($s,$c)>1?$c:'';$x++;}
with asumption $s = 'helloworld'.
Perl, 28 24 characters (includes 1 for 'p' option)
s/./$&x(s!$&!$&!g>1)/eg
Usage:
> perl -pe 's/./$&x(s!$&!$&!g>1)/eg'
helloworld
llool
At first I thought I could do this with negative look-ahead and negative look-behind, but it turns out that negative look-behinds must have a fixed length. So I went for nested regexes instead. With thanks to mob for the $& tip.
Ocaml, 139 133
Uses ExtLib's ExtString.String
open ExtString.String
let f s=let g c=fold_left(fun a d->a+Obj.magic(d=c))0 s in replace_chars(fun c->if g c=1 then""else of_char c)s
Non-golfed version
open ExtString.String
let f s =
let g c =
fold_left
(fun a c' -> a + Obj.magic (c' = c))
0
s
in replace_chars
(fun c ->
if g c = 1
then ""
else of_char c)
s
The function g returns the number of occurences of c in the string s. The function f replaces all chars either by the empty string or the string containing the char depending on the number of occurences. Edit: I shortened the code by 6 characters by abusing the internal representation of bools :-)
Oh, and ocaml is 0 on the TIOBE index ;-)
K, 18
{x@&x in&~1=#:'=x}
PHP - 83 78
<?for($a=$argv[1];$i<strlen($a);$r[$a[$i++]]++)foreach($ras$k=>$c)if($c>1)echo$k
Improved version:
<?for($s=$argv[1];$x<strlen($s);$c=$s[$x++]) echo substr_count($s,$c)>1?$c:'';
Of course this needs notices to be turned off
Edit: Improvement inspired by @hengky mulyono
I am so bad at codegolf :)
PHP - 137
Code
implode('',array_intersect(str_split($text),array_flip(array_filter(array_count_values(str_split($text)),function($x){return $x>=2;}))));
Normal Code
$text = 'helloworld';
$filter = array_filter(array_count_values(str_split($text)), function($x){return $x>=2;});
$output = implode('',array_intersect(str_split($text),array_flip($filter)));
echo $output;
C# – 77 characters
Func<string,string>F=s=>new string(s.Where(c=>s.Count(d=>c==d)>1).ToArray());
If you accept the output as an array, it boils down to 65 characters:
Func<string,char[]>F=s=>s.Where(c=>s.Count(d=>c==d)>1).ToArray();
Ruby 46 40 36
gets.chars{|c|$><<c if$_.count(c)>1}
J, 12 characters
Having entered a valid Perl answer, here's an invalid (language not in the TIOBE top 20) answer.
a=:#~1<+/@e.
Usage:
a 'helloworld'
llool
Declares a verb a which outputs only non unique items.
(GolfScript, 15 13 characters)
:;{.;?);>?)},
GolfScript is not one of the top 20, but a codegolf without GolfScript... (run it yourself)
Previous Version: (run script)
1/:;{;\-,;,(<},
GolfScript (14 chars)
:x{{=}+x\,,(},
Might not qualify to win, but it's useful to have a yardstick.
Perl (55)
@x=split//,<>;$s{$_}++for@x;for(@x){print if($s{$_}>1)}
Reads from stdin.
Mathematica 72 63
Ok, Mathematica isn't among the top 20 languages, but I decided to join the party anyway.
x is the input string.
"" <> Select[y = Characters@x, ! MemberQ[Cases[Tally@y, {a_, 1} :> a], #] &]
Perl 44
$l=$_;print join"",grep{$l=~/$_.*$_/}split""
Execution:
perl -lane '$l=$_;print join"",grep{$l=~/$_.*$_/}split""' <<< helloworld
llool
Python (56)
Here's another (few chars longer) alternative in Python:
a=raw_input();print''.join(c for c in a if a.count(c)>1)
If you accept output as a list (e.g. ['l', 'l', 'o', 'o', 'l']), then we could boil it down to 49 characters:
a=raw_input();print[c for c in a if a.count(c)>1]