using System;
class UniqueDigits
{
// n % 10 extracts the last digit.
// 1 << digit creates a bitmask for that digit.
// (mask & bit) checks whether the bit for this digit is already set.
// Mark the digit as seen: mask |= bit; This sets the bit for the current digit.
static bool AllDigitsUnique(int n) {
int mask = 0;
while (n > 0) {
int digit = n % 10;
int bit = 1 << digit;
if ((mask & bit) != 0) // digit already seen
return false;
mask |= bit; // mark the digit as seen
n /= 10;
}
return true;
}
static void Main()
{
int n = 123456;
Console.WriteLine(AllDigitsUnique(n) ? "Unique" : "Not unique");
n = 123452;
Console.WriteLine(AllDigitsUnique(n) ? "Unique" : "Not unique");
}
}
/*
run:
Unique
Not unique
*/