# Determine if all decimal digits are unique

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.

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

COBOL 66 with Object Cobol extensions

## Microfocus Cobol (721659 592)

       method-id u.
local-storage section.
77  i pic s9(9) comp.
77  c pic 9 occurs 10 value 0.
77  g pic 9.
01  s pic 9(9).
01  t redefines s pic 9 occurs 9.
77  n pic s9(9) comp.
77  d pic x.
88  f value is 'y'.
procedure division using by value n returning d.
move n to s. move 'n' to d. perform p varying i from 1 by 1 until i greater than 9 or f. goback.
p.  move t (i) to g. if c (g) is not zero then move 'y' to d. add 1 to c (g).
end method.


Hmmm, I think I should go back to pitch & putt :(

EDIT: OMGZ I CAN USE LOWER CASE!!!

# C++ 82

this code is at least legitimate

int a(uint32_t i,uint32_t r=0,uint32_t h=0)
{return i?r&h?0:a(i/10,r|h,1<<i%10):1;}        // thanks to DreamWarrior


# C 47

this works if you compile it in debug mode or provide two more arguments set to 0

a(i,r,h){return i?r&h?0:a(i/10,r|h,1<<i%10):1;}


Regular Expression 17 bytes

^(?:(.)(?!.*\1))*$ Original expression taken from https://stackoverflow.com/a/12870549. # Clojure, 23 #(=(distinct x)(seq x))  Usage: (#(=(distinct x)(seq x)) "23563462464543") # 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  # 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  # 05AB1E, 2 bytes ÙQ  Try it online! -1 thanks to Kaldo { # Sort. ¥ # Consecutive differences. P # Product.  Works on negatives, though OP never specified... • ÙQ for 2 bytes. – Kaldo Apr 5 '18 at 15:25 # 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 :) # Japt, 5 3 bytes ¶ìâ  Try it here # 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. # 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  # JavaScript, 22 bytes s=>!s[new Set(s).size]  Try it online # 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: Download and test DU.COM. # Husk, 4 bytes S=ud  Try it online! S # Hook: S fgx means f x(gx) = # are they equal: # x (implied by S) # and u # unique elements of x d # when x is the decimal digits of the input  # Pyth, 2 bytes q{  Try it online! # Julia, 25 bytes the best I could come up with was the same as one of the top voted answers n->10^length(Set("$n"))>n


Try it online!