title: Do two numbers contain same digits
body: <p>Ok, so I was wondering how can one check whether two numbers have the same digits, e.g. <code>21</code> and <code>12</code> are ok, also <code>1233</code> and <code>2313</code>, but <code>123</code> and <code>1233</code> are not, meaning that the digits are permutation of another number's digits.</p>
  
  <p>I know how to do it with arrays or strings or maps, but <em>the problem</em> is, that I don't want to do it with either of those, <strong>if there exists another solution</strong>.</p>
  
  <p>The solution with arrays / maps:</p>
  
  <pre><code>Map&lt;int, int&gt; counter = new HashMap&lt;int, int&gt;();
  for (int i = 0; i &lt; 10; i++)
      counter.put(i, 0);
  int x = 2421, y = 4223; // testcase
  while (x &gt; 0 || y &gt; 0) {
      if (x == 0 || y == 0) // if they are not the same length, one will be 0 and thus they are not permutations
          return false;
      counter.put(x%10, counter.get(x%10) + 1);
      counter.put(y%10, counter.get(y%10) - 1);
      x /= 10;
      y /= 10;
  }
  // For each digit we added 1 to the counter if it was found in `x`
  // and subtract 1 if it was found in `y`.
  return counter.values() == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  </code></pre>
  
  <p>Now, the array approach is completely the same, since using a map for digits <code>0-9</code> is the same as using the key for map as the index in array. The solution without any data structure looks far from ideal to me:</p>
  
  <pre><code>private static boolean haveSameDigits(int x, int y) {
      // Because we are not allowed to use maps, declare 10 vars...
      int c0 = 0;
      int c1 = 0;
      int c2 = 0;
      int c3 = 0;
      int c4 = 0;
      int c5 = 0;
      int c6 = 0;
      int c7 = 0;
      int c8 = 0;
      int c9 = 0;
  
      while (x &gt; 0 || y &gt; 0) {
        if (x == 0 || y == 0)
            return false;
        if ((x % 10) == 0)
            c0++;
        else if ((x % 10) == 1)
            c1++;
        else if ((x % 10) == 2)
            c2++;
        else if ((x % 10) == 3)
            c3++;
        else if ((x % 10) == 4)
            c4++;
        else if ((x % 10) == 5)
            c5++;
        else if ((x % 10) == 6)
            c6++;
        else if ((x % 10) == 7)
            c7++;
        else if ((x % 10) == 8)
            c8++;
        else if ((x % 10) == 9)
            c9++;
        if ((y % 10) == 0)
            c0--;
        else if ((y % 10) == 1)
            c1--;
        else if ((y % 10) == 2)
            c2--;
        else if ((y % 10) == 3)
            c3--;
        else if ((y % 10) == 4)
            c4--;
        else if ((y % 10) == 5)
            c5--;
        else if ((y % 10) == 6)
            c6--;
        else if ((y % 10) == 7)
            c7--;
        else if ((y % 10) == 8)
            c8--;
        else if ((y % 10) == 9)
            c9--;
  
        x /= 10;
        y /= 10;
      }
  
      return c0 == 0 &amp;&amp; c1 == 0 &amp;&amp; c2 == 0 &amp;&amp; c3 == 0 &amp;&amp; c4 == 0 &amp;&amp; c5 == 0 &amp;&amp; c6 == 0 &amp;&amp; c7 == 0 &amp;&amp; c8 == 0 &amp;&amp; c9 == 0
  }
  </code></pre>
  
  <p>I have googled about it but no matter what I typed I ended up with a solution using strings or arrays.</p>
  
  <p>I am not looking for a solution, I actually don't want it, I just need a hint to the approach.</p>
  
  <hr>
  
  <p>Adding some more information: There is nothing <strong>prohibiting</strong> me from using any data structure I want, this is my program and nobody will be checking over what I do. I am just that kind of person that likes to learn new stuff, so I was wondering if there is a quick solution to it.</p>
  
  <p>As stated in the comments, one can iterate over both numbers and check for each number in range (0,9) inclusive, how many times they appear in string but that obviously yields time complexity of <code>O(n*n)</code> which is not optimal. </p>