Type uniqchars!
CJam, 3 bytes
qL|
Setwise or of the input with an empty list. CJam set operations preserve element order.
Try it online
C# 6, 18 + 67 = 85 bytes
Requires this using
statement:
using System.Linq;
The actual method:
string U(string s)=>string.Concat(s.Where((x,i)=>s.IndexOf(x)==i));
This method saves some chars by defining the function as a lambda, which is supported in C# 6. This is how it would look in C# pre-6 (but ungolfed):
string Unique(string input)
{
return string.Concat(input.Where((x, i) => input.IndexOf(x) == i));
}
How it works: I call the Where
method on the string with a lambda with two arguments: x
representing the current element, i
representing the index of that element. IndexOf
always returns the first index of the char passed to it, so if i
is not equal to the first index of x
, it's a duplicate char and mustn't be included.
GolfScript, 2 bytes
.&
or, alternatively:
.|
I posted this a while ago in the Tips for golfing in GolfScript thread. It works by duplicating the input string (which is put on the stack automatically by the GolfScript interpreter, and which behaves in most ways like an array of characters) and then taking the set intersection (&
) or union (|
) of it with itself. Applying a set operator to an array (or string) collapses any duplicates, but preserves the order of the elements.