37
\$\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\$
  • \$\begingroup\$ I'd still be interested in other C or C++ solutions as per the question that inspired this one. \$\endgroup\$ – Thomas May 21 '14 at 19:59
  • 1
    \$\begingroup\$ Why no C or C++ macros or undefined behavior? That's oddly limiting to just two languages. \$\endgroup\$ – dfeuer Apr 14 at 2:05

71 Answers 71

0
\$\begingroup\$

Clojure, 23

#(=(distinct x)(seq x))

Usage: (#(=(distinct x)(seq x)) "23563462464543")

\$\endgroup\$
0
\$\begingroup\$

Candy, 13 bytes

This is a late post, but...

(k&{1k1.|0})0

Invokation with -I , which is actually for string parameters.

The long form:

while    # while we are still consuming characters from the stack
  setSP  # pop the next stack-id from the stack.  It's the ord of the digit
  stackSz  # test for empty stack
  if
    digit1 setSP  # goto stack #1, and push the true indicator on
    digit1
    retSub        # and halt
  else
    digit0    # mark the stack as having been visited
  endif
endwhile
digit0    # push the false indicator if we got this far
\$\endgroup\$
0
\$\begingroup\$

Prolog, 49 bytes

Code:

p(N):-number_codes(N,L),sort(L,S),msort(L,T),S=T.

Explained:

p(N):-number_codes(N,L), % Convert int to list (of charcodes)
      sort(L,S),         % Sort list and remove duplicates
      msort(L,T),        % Sort list and keep duplicates
      S=T.               % Check if lists are equal

Example:

p(48778584).
false

p(17308459).
true
\$\endgroup\$
0
\$\begingroup\$

Add++, 7 bytes

L,BDdq=

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 2 bytes

ÙQ

Try it online!

-1 thanks to Kaldo


{   # Sort.
 ¥  # Consecutive differences.
  P # Product.

Works on negatives, though OP never specified...

\$\endgroup\$
  • \$\begingroup\$ ÙQ for 2 bytes. \$\endgroup\$ – Kaldo Apr 5 '18 at 15:25
0
\$\begingroup\$

Python 3, 29 bytes

lambda x:10**len({*str(x)})>x

Inspired on this answer in Python 2; for Python 3 to only lag behind 2 by one byte is pretty rare :)

\$\endgroup\$
0
\$\begingroup\$

Japt, 5 3 bytes

¶ìâ

Try it here

\$\endgroup\$
0
\$\begingroup\$

C# (Visual C# Interactive Compiler), 28 bytes

i=>(i+"").ToDictionary(c=>c)

Try it online!

This lambda uses presence or absence of an exception to determine whether a number has unique digits. If a digit occurs more than once, a duplicate key exception will be thrown when converting the string to a dictionary.

\$\endgroup\$
0
\$\begingroup\$

Braingolf, 12 bytes

dlMulMve1:0|

Try it online!

Explanation

dlMulMve1:0|  Implicit input from args

d             Split into digits
 lM           Push length of stack to next stack
   u          Keep only first occurrence of each unique digit
    lM        Push length of stack to next stack
      v       Switch to next stack
       e      If top 2 items are equal
        1      - Push 1
         :    Else
          0    - Push 0
           |  EndIf

              Implicit output of top of stack
\$\endgroup\$
0
\$\begingroup\$

JavaScript, 22 bytes

s=>!s[new Set(s).size]

Try it online

\$\endgroup\$
0
\$\begingroup\$

8086 machine code, 9 bytes

51 AC 8B FE F2 AE 59 E0 F7

Unassembled listing:

    DU  MACRO
    SEARCH: 
51      PUSH CX             ; save loop counter 
AC      LODSB               ; load next digit from SI into AL, advance SI 
8B FE   MOV  DI, SI         ; DI is offset of next digit 
F2/ AE  REPNZ SCASB         ; search until match, ZF=1 if found 
59      POP  CX             ; restore loop counter 
E0 F7   LOOPNZ SEARCH       ; loop until match found or CX is 0
        ENDM

Implemented as a MACRO (most similar to function). Input is SI, length in CX. Output is ZF=0 if unique, ZF=1 if not unique. Also returned in CX is position from lowest order digit where first duplicate was found and SI the duplicated digit.

Below is a test program in IBM PC DOS that reads input from command line args, displays T or F:

enter image description here

Download and test DU.COM.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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