How to improve: Given two integers, return the number of digits that they share
<p>I received this question during an interview, the question is </p>

<blockquote>
  <p>Given two integers, return the number of digits that they share.</p>
</blockquote>

<p>For example 129 and 431 would return 1 - as they both share the digit <code>1</code>, but no other digit. 95 and 780 would return 0, since none of the integers overlap. </p>

<p>My thoughts are just iterate through the digits, store them into a hashtable and check <code>.containsKey.</code></p>

<p><strong>My Java solution:</strong></p>

<pre><code>public int commonDigits(int x, int y) {
     int count = 0;
     HashTable&lt;Integer, String&gt; ht = new HashTable&lt;Integer, String&gt;();

     while (x != 0) { 
         ht.put(x % 10, "x");
         x /= 10;
     }

     while (y != 0) {
         if ((ht.containsKey(y % 10)) {
             count++;
         }
         y /= 10;
     }

    return count;
}
</code></pre>

<p>But this takes up O(n) space and O(n + m) time, anyways I can improve this?</p>