1

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.

2
  • Hint: There's a std::unique algorithm. Commented Mar 6, 2014 at 22:28
  • I'd use std::to_string to convert the integer to string, then compare the digits. Commented Mar 6, 2014 at 22:36

1 Answer 1

3
 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I was thinking of. Works perfectly!
it does? I'm surprised actually, I didn't even test it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.