# 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.
def all_digits_unique(n: int) -> bool:
mask = 0
while n > 0:
digit = n % 10
bit = 1 << digit
if mask & bit:
return False # digit already seen
mask |= bit
n //= 10
return True
if __name__ == "__main__":
n = 123456
print("Unique" if all_digits_unique(n) else "Not unique")
n = 123452
print("Unique" if all_digits_unique(n) else "Not unique")
'''
run:
Unique
Not unique
'''