47
\$\begingroup\$

Deleted questions on Stack Overflow sometimes make for great golf material.

Write a function that takes a nonnegative integer as input, and returns true if all the digits in the base 10 representation of that number are unique. Example:

48778584 -> false
17308459 -> true

Character count includes only the function.

If you choose to answer in C or C++: no macros, no undefined behaviour; implementation-defined behaviour and compiler warnings are fine.

\$\endgroup\$
2
  • \$\begingroup\$ I'd still be interested in other C or C++ solutions as per the question that inspired this one. \$\endgroup\$
    – Thomas
    Commented May 21, 2014 at 19:59
  • 11
    \$\begingroup\$ Why no C or C++ macros or undefined behavior? That's oddly limiting to just two languages. \$\endgroup\$
    – dfeuer
    Commented Apr 14, 2019 at 2:05

92 Answers 92

1 2 3
4
0
\$\begingroup\$

convey, 355 bytes

          1-}  1-}  1-}  1-}  1-}  1-}  1-}  1-}  1-}  1-}
>>vv<">>~>>?;~>>?;~>>?;~>>?;~>>?;~>>?;~>>?;~>>?;~>>?;~>>?;}
^ ;<1@>]3 >>!3 >>!3 >>!3 >>!3 >>!3 >>!3 >>!3 >>!3 >>!3 >>!
,{>">"   >#v0 >#v0 >#v0 >#v0 >#v0 >#v0 >#v0 >#v0 >#v0 >#v0
.10|>#>>"@^>#"@^>#"@^>#"@^>#"@^>#"@^>#"@^>#"@^>#"@^>#"@^>#]
_<<<<%10>=0 1>=1 1>=2 1>=3 1>=4 1>=5 1>=6 1>=7 1>=8 1>=9 1

Try it online!

I don't believe that this was the more optimal solution in convey, but it's cool to see it running at least (You can watch all the process in the "Try it online" link):

enter image description here

\$\endgroup\$
-1
\$\begingroup\$

Perl, 63ish

$ echo 48778584| perl -F// -alpe '$"="";$_= 0<"@{[map {$a{$_}++} @F]}"?"false":"true"'
false
$ echo 17308459| perl -F// -alpe '$"="";$_= 0<"@{[map {$a{$_}++} @F]}"?"false":"true"'
true
\$\endgroup\$
1 2 3
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.