How to find the common digits in two integer?

Aug 29, 2014 at 11:59am
Suppose I have two integer number 4578 and 5908.
Now I have to find the common digits for these two integers.
For the integers given above the output should be 58 (because 5 & 8 are common in both integers.)

How many ways there are to do this?
What will be the most efficient way?
Aug 29, 2014 at 12:07pm
Does the order matter? (I.e. do 590 and 935 have two?)
How about repeats? How many do 54 and 55 share?

Of course there are many ways. Efficiency can depend on the input size too.


Edit: http://www.cplusplus.com/reference/algorithm/set_intersection/
Last edited on Aug 29, 2014 at 12:18pm
Aug 29, 2014 at 12:10pm
You can first store the digits in two vectors and then compare each elements , if there's a match print it.
Extracting the digits is easy as well , note that the last digit could be extracted by doing mod 10 , then divide the number by 10 to remove the last digit.
So these are the hints .
Aug 29, 2014 at 2:33pm
How many ways there are to do this?

How many programmers are there?

Convert the two numbers to strings, then check the two strings.
1
2
3
4
5
6
  string s1;
  string s2;

  s1 = to_string (4578);  // Requires C++11
  s2 = to_string (5908);
  // Now iterate through the digits in s1 and see if they exist in s2 

Topic archived. No new replies allowed.