I want to write a function that compares the numbers in an array and deletes those who contain the same digits (ex. 1335 531) .The deleting part arouses no problems but I can't seem to figure out how to compare them digit by digit, especially when they don't have the same length. Any idea is more that welcomed and appreciated.
1 Answer
unsigned get_digit_mask(unsigned input) { unsigned result = 0; unsigned digit; do { digit = input%10; //get the rightmost digit input/=10; //remove it from the number result |= (1<<digit); //set that bit of the result }while(input); //continue as long as there's more digits return result; //return bitmask of used digits } If you use this function on the number 1335, it will return a mask with the 1st, 3rd, and 5th bits set. If you give the function the number 531, it will return a mask with the 1st, 3rd, and 5th bits set. If the masks are equal, then the numbers contain the same digits.
2 Comments
Georgiana.b
Exactly what I was thinking of. Works perfectly!
Mooing Duck
it does? I'm surprised actually, I didn't even test it
std::uniquealgorithm.std::to_stringto convert the integer to string, then compare the digits.