title: Match same values from two distinct arrays
body: <p>Imagine we have 2 arrays that have the exact same elements (not in the same order) but, as a restriction, we can only compare elements of array A to elements of array B (meaning we can't sort them individually and then print out the result). And the goal is to match the elemnts of A and B and sort sort it in ascending order. So with these 2 arrays as input:
  A = [5, 8, 1, 2, 4, 3, 7, 6] and B = [1, 5, 2, 6, 8, 4, 7, 3]</p>
  <p>The output should be like</p>
  <pre><code>A 1 2 3 4 5 6 7 8
  B 1 2 3 4 5 6 7 8
  </code></pre>
  <p>The only solution I can think of right now is</p>
  <pre><code>    for (int i = 0; i &lt; len; i++)
          for (int j = i; j &lt; len; j++)
              if (A[i] == B[j])
                  swap(A, i, j);
  
      for (int i = 0; i &lt; len; i++)
          for (int j = i; j &lt; len; j++)
              if (A[i] &gt; B[j])
                  swap(A, i, j);
                  swap(B, i, j);
  </code></pre>
  <p>That would take too long with big outputs though. Is there an efficient algorithm to use here? That makes it faster than O(n^2)</p>