Java ( 131 59 57)
57 characters:
removed ^ and $ as @n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ suggested
boolean u(int n){return !(n+"").matches(".*(.).*\\1.*");}
59 characters (works also with negative numbers!):
boolean u(int n){return !(n+"").matches("^.*(.).*\\1.*$");}
79 78 characters (thanks @n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ ):
Use for loop to save a few charachers and use int as a boolean array.
Use & instead of && to save 1 character (It turns out that java allows it).
boolean u(int n){for(int x=0;n>0&(x>>n%10&1)==0;n/=10)x|=1<<n%10;return n==0;}
131 characters (returns true for negative numbers):
boolean u(int n){int[] p=new int[]{2,3,5,7,11,13,17,19,32,29};double a=9001312320D;while(n>0){a/=p[n%10];n/=10;}return (long)a==a;}
with comments:
boolean unique(int n){
int[] p=new int[]{2,3,5,7,11,13,17,19,32,29};//list of 10 first primes
double a=9001312320D;//10 first primes multiplied
while(n>0){
a/=p[n%10];//divide by (n%10+1)th prime
n/=10;//divide n by 10, next digit
}
return (long)a==a;//if a is integer then n has all digits unique
}
And answer that is technically correct (character count includes only the function, not global variables), but I think it's cheating, 29 characters:
boolean u(int i){return m[i];}
m[] is boolean array that contains correct answers for all 32-bit integers.
1Why no C or C++ macros or undefined behavior? That's oddly limiting to just two languages. – dfeuer – 2019-04-14T02:05:57.200
I'd still be interested in other C or C++ solutions as per the question that inspired this one. – Thomas – 2014-05-21T19:59:58.443