41
1
Given a string consisting of printable ASCII chars, produce an output consisting of its unique chars in the original order. In other words, the output is the same as the input except that a char is removed if it has appeared previously.
No built-ins for finding unique elements in an array can be used (for example, MATLAB has a unique
function that does that). The idea is to do it manually.
Further details:
- Either functions or programs are allowed.
- Input and output can be in the form of function arguments, stdin/stdout (even for functions), or a mix of those.
- If stdin or stdout are used, a string is understood as just the sequence of chars. If function arguments are used, the sequence of chars may need to be enclosed in quotation marks or equivalent symbols that the programming language of choice uses for defining strings.
- The output should be a string containing only the unique characters of the input. So no extra linebreaks, spaces etc. The only exception is: if the output is displayed in stdout, most displaying functions add a trailing
\n
(to separate the string from what will come next). So one trailing\n
is acceptable in stdout. - If possible, post a link to an online interpreter/compiler so that others can try your code.
This is code golf, so shortest code in bytes wins.
Some examples, assuming stdin and stdout:
Input string:
Type unique chars!
Output string:
Type uniqchars!
Input string
"I think it's dark and it looks like rain", you said
Output string
"I think'sdarloe,yu
Input string
3.1415926535897932384626433832795
Output string
3.14592687
5Just to double check: Does the no builtins rule mean that set objects are disallowed? – Sp3000 – 2015-10-04T14:51:36.113
@Sp3000 Set objects are allowed. Just don't use a function or method (if it exists) that gives you its unique elements. And input/output should be strings, not set tobjects – Luis Mendo – 2015-10-04T14:54:07.870
@Sp3000 Do you think it would make it more interesting to reduce byte count by half if no set functions are used? Or better not change the rules once the challenge has been set? – Luis Mendo – 2015-10-04T15:08:01.550
5I think only my answer uses sets currently, and I wouldn't mind if you changed it. However, I'm not really sure a bonus like that would change much, e.g. I doubt CJam would be doable in < 6 bytes without sets. Also, I'm not sure where the line is between a builtin which finds unique elements, and constructing a set from a number of elements... – Sp3000 – 2015-10-04T15:13:11.510
1@Sp3000 Yes, it's a blurred border. I hadn't anticipated set functions. I think I'll leave the challenge as it is now – Luis Mendo – 2015-10-04T15:16:08.020
@ThomasKwa I agree with you. In fact, when an interpreter prints a string to the screen, there's usually an implicit
\n
printed at the end even if the string doesn't have it. I've notified J F – Luis Mendo – 2015-10-05T09:53:10.367Is it allowed for the input data structure to have a different type from the output data structure, if they both are valid representations of strings of characters? – CR Drost – 2015-10-06T18:07:42.163
I guess if they both represent strings that's fine. But I'm confused: how can they represent strings without actually being string data type? – Luis Mendo – 2015-10-06T20:34:27.110
@LuisMendo: I mean the easiest example might be JavaScript where
"abc".split("")
is a data structure which represents a string without actually being one. (Some of the below answers might be able to use this to shave 10 or more bytes off!) The case that I am talking about is a lot more convoluted and appears below in my Haskell answer. – CR Drost – 2015-10-07T00:44:29.173@Sp3000 I totally missed than answer. Thanks for the heads-up! – Luis Mendo – 2015-10-09T10:10:07.893
1-byte cheating Jelly solution:
Q
. – Erik the Outgolfer – 2016-09-28T14:46:30.740@EriktheGolfer Doubly cheating – Luis Mendo – 2016-09-28T14:51:41.433
@EriktheGolfer Because the language postdates the challenge and because builtin is not allowed – Luis Mendo – 2016-09-28T14:53:25.743
@LuisMendo Non-competing is not always cheating. Non-competing does not compete at all, so what sort of cheating is it? – Erik the Outgolfer – 2016-09-28T14:54:01.443
Ah, finally a use for the pangram "Cwm fjord bank glyphs vext quiz." Or for my favorite word, Abcdefghijklmnopqrstuvwxyz
– Draco18s no longer trusts SE – 2017-05-17T15:45:21.227-1 Disallowing built-ins serves no purpose other than making interesting answers. This is code-golf, not popularity contests. – MilkyWay90 – 2019-04-06T02:26:39.920