how do i check if a number has only unique digits?

for example, 2345 is a unique digit number because no digit is shown twice but 3324 isn't a unique number because 3 is there twice. I tried using % but I as (code) shows but I didn't get digits I got numbers, edit: you cant use strings

number%10==number%100||number%10==number%1000||number%100==number%1000

You could use an array of 10 flags to indicate whether a digit has been seen yet. Work your way through the number in a loop, extracting the units digit, checking whether the digit has been seen, flagging the digit as seen, and dividing the number by 10 for the next iteration.

As pointed out by @Bathsheba, some care is needed when dealing with negative numbers.

For example:

int unique(long long int number)
{
    char seen[10] = {0};

    while (number) {
        int digit = number % 10;

        number /= 10;
        if (digit < 0) {
            /* 
             * The number was negative.  Make it positive.
             * (Note: Checking the number is negative before the while
             * loop could fail when number is LLONG_MIN, so do it here
             * instead.)
             */
            digit = -digit;
            number = -number;
        }
        if (seen[digit]++)
            return 0; /* not unique */
     }
     return 1; /* unique */
 }

Separate functions may be needed if it is necessary to deal with both long long int and unsigned long long int, but the one that handles long long int can make use of the one that handles unsigned long long int as follows:

#include <limits.h>

int unique_ull(unsigned long long int number)
{
    char seen[10] = {0};

    while (number) {
        int digit = number % 10;

        number /= 10;
        if (seen[digit]++)
            return 0; /* not unique */
     }
     return 1; /* unique */
 }

int unique_ll(long long int number)
{
    unsigned long long int n;

    /* Represent original number as a 2's complement number. */
    n = number;
    if (n > LLONG_MAX) {
        /*
         * Original number was negative, so take its 2's complement to "negate" it.
         * (Note: This works even when original number is LLONG_MIN.)
         */
        n = -n;
    }
    /* Handle as an unsigned long long int. */
    return unique_ull(n);
 }

I suppose it would be useful to support intmax_t and uintmax_t in the same way:

#include <stdint.h>

int unique_um(uintmax_t number)
{
    char seen[10] = {0};

    while (number) {
        int digit = number % 10;

        number /= 10;
        if (seen[digit]++)
            return 0; /* not unique */
     }
     return 1; /* unique */
 }

int unique_m(intmax_t number)
{
    uintmax_t n;

    /* Represent original number as a 2's complement number. */
    n = number;
    if (n > INTMAX_MAX) {
        /*
         * Original number was negative, so take its 2's complement to "negate" it.
         * (Note: This works even when original number is INTMAX_MIN.)
         */
        n = -n;
    }
    /* Handle as a uintmax_t. */
    return unique_um(n);
 }

Numbers having Unique (or Distinct) digits, Sum of the digits of square of the given number which has only 1's as its digits � priya_1998. Check out this Author's contributed articles. If you like� How do I find the number of unique digits in a given number. e.g if 'input1' is 252 it must return 1, since 5 is the non-repeating digit; if 'input1' is 25000 it must return 2, since digits 2 and 5 are the only non-repeating ones. Is there a better way to solve this problem?

My five cents.:)

For starters the parameter of the function should have the type long long int (or intmax_t). In this case the function can be applied to objects of any signed integer type.

Here is a demonstrative program.

#include <stdio.h>

int unique_digits( long long int n )
{
    const long long int Base = 10;

    int unique = 1;

    while ( unique && n )
    {
        long long int digit = n % Base;
        long long int tmp = n /= Base;

        while (tmp && digit != tmp % Base ) tmp /= Base;

        unique = tmp == 0;
    }

    return unique;
}

int main(void) 
{
    int n = 12345678;

    printf( "%d: %s\n", n, unique_digits( n ) ? "unique" : "non_unique" );

    n = 12345671;

    printf( "%d: %s\n", n, unique_digits( n ) ? "unique" : "non_unique" );

    n = -12345678;

    printf( "%d: %s\n", n, unique_digits( n ) ? "unique" : "non_unique" );

    n = -112345671;

    printf( "%d: %s\n", n, unique_digits( n ) ? "unique" : "non_unique" );

    return 0;
}

The program output is

12345678: unique
12345671: non_unique
-12345678: unique
-112345671: non_unique

If to include the header <stdint.h> then the type long long int can be substituted for the type intmax_t.

Another approach is to use an integer as an array of digits. Here is a demonstrative program.

#include <stdio.h>

int unique_digits( long long int n )
{
    const long long int Base = 10;

    unsigned short unique = 0;

    long long int digit;
    int offset;

    do
    {
        digit = n % Base;
        if ( digit < 0 ) digit = -digit;

        offset = 1 << digit;
        unique ^= offset;
    } while ( ( unique & offset ) && ( n /= Base )  );        

    return n == 0;
}

int main(void) 
{
    int n = 12345678;

    printf( "%d: %s\n", n, unique_digits( n ) ? "unique" : "non_unique" );

    n = 12345671;

    printf( "%d: %s\n", n, unique_digits( n ) ? "unique" : "non_unique" );

    n = -12345678;

    printf( "%d: %s\n", n, unique_digits( n ) ? "unique" : "non_unique" );

    n = -112345671;

    printf( "%d: %s\n", n, unique_digits( n ) ? "unique" : "non_unique" );

    return 0;
}

It output is the same as of the previous program

12345678: unique
12345671: non_unique
-12345678: unique
-112345671: non_unique

Next Number with distinct digits, Rebmu's "mushing" trick is that it's case-insensitive, so characters are run together. Whenever a case Uses division to count the number of occurrences of each digit. Call by Also, make sure the rest of your program uses only registers > 71. Given k digits, the number of unique numbers is 9*9*8*…*(11-k). For example 100-999 will have 9 possibilities for the first position (anything excluding 0), 9 for the second (anything excluding the first digit) and 8 for the third (anything excluding the first and second digits).

You can use set STL in order to check if a number has only unique digits. For example, let us consider number 2020, then we can convert the number into the string,

int num;
cin>>num
string s = to_string(num);

After that, initialise a set with the contents of string s.

set<int> uniDigits(s.begin(), s.end());

Then we can compare the size of string s and newly created set uniDigits.

if(s.size() == uniDigits.size()) { break; }

As we know the property of sets, that set stores only unique numbers and discards repeated digits, then if both sizes are equal that means all digits are unique. If size of set is less than that of the string, that means the number does not contain distinct digits and all the repeated numbers are discarded.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s = "2020";
    set<int> digits(s.begin(), s.end());
    cout << s.size() << " " << digits.size() << endl;
    return 0;
}

Here, the size of string is 4 and size of set is 2, because the string contains two repeated digits.

Determine if all decimal digits are unique, What is the best and quickest way to check if an integer number is divisible by 17 ? 1,237 Views � I'm thinking of a 10-digit integer whose digits are distinct. there exists a positive integer m which is a multiple of n that can only be written with� how do i check if a number has only unique digits?-3. How to count total appearance of 0 or 1 from range number 1 to n? 0. C Language - add products’ digits (i.e

Here's one approach to achieve this

#include <stdio.h>

int main(){
    int n, i, digit;
    static int arr[10];
    scanf("%d", &n);
    while(n>0){
        digit = n%10;
        arr[digit]++;
        n=n/10;
    }
    for(i=0; i<10; i++){
        if(arr[i]>1){
            printf("Not unique!");
            break;
        }
    }
    if(i==10){
        printf("unique");
    }
    return 0;
}

Note that the elements of static array is initialized with the default value of 0.

How to check if a given integer has at least one repeated digit, Here is a java example that counts unique digits in a number. Source: (Example. java). public class Example { public static void main� Using python, count the number of digits in a number. In this tutorial, we will learn how to count the total number of digits in a number using python. The program will get the input from the user and print out the result.We will show you two different ways to calculate total digits in a number.

Count unique digits in a number, A numerical digit is a single symbol (such as "2" or "5") used alone, or in combinations (such as A positional number system has one unique digit for each integer from zero up to, of the digit from the separator; the value of n is positive (+), but this is only if the digit is to the left of the separator. Read � Edit � View history� The last Java System.out.format statement will print the number of digits present in the given number as output. System.out.format(" Number of Digits in a Given Number = %d", Count); The output of given variable 1465 is 4. Java Program to Count Number of Digits in a Number using For Loop

Numerical digit, 4th can only be on of 6 numbers. 9*8*7*6 = 3024 numbers have all four distinct digits. Distinct digits between 100 and 999. How many� Enter an integer: 3452 Number of digits: 4 The integer entered by the user is stored in variable n . Then the while loop is iterated until the test expression n! = 0 is evaluated to 0 (false).

Testing 4 numbers to see if its has 3 unique numbers, The integer entered by the user is stored in variable n . Then the while loop is iterated until the test expression n! = 0 is evaluated to 0 (false). After the� Check if the Xor of the frequency of all digits of a number N is zero or not; Check if two numbers have same number of digits; Check if the frequency of all the digits in a number is same; Check if the given decimal number has 0 and 1 digits only; Check if all digits of a number divide it; Check if the sum of digits of a number N divides it

Comments
  • Hints, define an array int visited[10] = {0}. Then populate this by extracting the rightmost digit using % 10 and removing that digit using / 10. The treatment of superfluous leading zeros might need some thought.
  • stackoverflow.com/questions/3118490/… does this help?
  • hmmm... 11 in binary is represented as 1011, in hexadecimal as b
  • Needs thought if number is negative. (Personally I'd change the type to unsigned long long.)
  • @Bathsheba Thanks, I added a safe correction for negative numbers. I guess it needs a different function for unsigned long long in case someone actually does need to use it with negative long long numbers.