Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this in a similar way to the simpler cases of one and two different values.</p> <p>We need two integers for each bit of the numbers (e.g. 32 bits). For each number, if that bit is zero, XOR the first integer with it. If it isn't, XOR the second integer with it.</p> <p>Also, keep count of how many times you find a 1 or 0 in each position (we only need to check if this is even or odd, so keep a boolean).</p> <p>After iterating through, our pairs of integers will be one of the following. The first number here represents an even count, the second an odd.</p> <pre><code>0, a^b^c a^b, c a^c, b b^c, a </code></pre> <p>For each pair, check the even count integer. If it is zero, then we know the other integer is a^b^c, since no two of our results will be equal. Otherwise, we've found a value at the odd count integer.</p> <pre><code>public static int[] find3(int[] list) { int[][] xors = new int[32][2]; boolean[] counts = new boolean[32]; for (int curr : list) { for (int i = 0; i &lt; 32; i++) { xors[i][(curr &amp; (1 &lt;&lt; i)) &gt;&gt; i] ^= curr; counts[i] ^= ((curr &amp; (1 &lt;&lt; i)) == (1 &lt;&lt; i)); } } // this really shouldn't take so many lines int[] ret = new int[3]; int found = 0; for (int i = 0; i &lt; 32; i++) { int oddCount = xors[i][counts[i] ? 1 : 0]; int evenCount = xors[i][counts[i] ? 0 : 1]; if (evenCount != 0) { // avoid the 0, a^b^c case. if (found == 0) { ret[0] = oddCount;// a ret[2] = evenCount;// b^c for now found++; } else if (found == 1 &amp;&amp; ret[0] != oddCount) { ret[1] = oddCount;// b ret[2] ^= oddCount;// (b^c)^b == c break; } } } return ret; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload