[Solved] How can I check if a number have the same digits? (in a func) [closed]


Not sure what you mean by [10] char array

If you want to see if a number has any digit in base 10 that appears twice you can use std::bitset or similar then use x % 10 to get a digit and x /= 10 until x becomes 0.

bool hasRepeatedDigit( unsigned int num, unsigned int base )
{
    std::vector< char > cache( base ); // avoiding vector<bool>

    while( num )
    {
       unsigned int digit = num % base;
       if( cache[ base ] )
          return true;

       cache[ base ] = 'T'; // just mark it
       num /= base;
    }

    return false;
}

This is generic and assumes you have a number as a number, not as a string. If it’s just a string it would be implemented differently.

2

solved How can I check if a number have the same digits? (in a func) [closed]