Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a few approaches you can take:</p> <ul> <li><strong>Approach 1</strong> &mdash; O(<em>n</em> log <em>n</em>): sort the array. Then, iterate over the elements of the sorted array, two at a time (<code>i=0</code>, <code>i=2</code>, etc.). When <code>a[i]</code> and <code>a[i+1]</code> are unequal &mdash; or when <code>i+1 == a.length</code> &mdash; you know that <code>a[i]</code> is unpaired.</li> <li><strong>Approach 2</strong> &mdash; O(<em>n</em><sup>2</sup>): iterate over the elements. For each element <code>a[i]</code>, iterate over the elements (in a nested loop) and see if it ever occurs that <code>a[i] == a[j]</code> while <code>i != j</code>. If not, then <code>a[i]</code> is unpaired.</li> <li><strong>Approach 3</strong> &mdash; O(<em>m</em>), where <em>m</em> is the difference between the greatest and the least element (noting that <em>m</em> is &Omega;(<em>n</em>)): iterate over the elements, finding the greatest and least values <code>MIN</code> and <code>MAX</code>. Create an <code>int[] b = new int[MAX-MIN+1]</code>. Iterate over the elements again, incrementing <code>b[a[i]-MIN]</code> for each element. Then iterate over <code>b</code>; when you find <code>b[j]==1</code>, <code>j</code> is unpaired.</li> </ul> <p><strong>Note:</strong> You use the term "element integer", but that's not a real term. The above assumes that you mean "integer-valued element". If you actually mean "element <em>index</em>", then only Approach 2 can be used without modification. Approach 3 would require a little bit of adjustment, and Approach 1 would require a lot of adjustment. (Of course, once you've found the <em>value</em> that occurs only once, you can just iterate over the array one more time to find the <em>index</em> of that value &mdash; provided you still have the original array order.)</p> <hr> <p><strong>Edited to add:</strong> I don't know how I missed this before &mdash; I guess I'm not used to thinking of bitwise operations when writing Java &mdash; but the best solution is actually:</p> <ul> <li><p><strong>Approach 4</strong> &mdash; O(<em>n</em>): compute the bitwise-XOR, <code>^</code>, of all the elements of the array. This is the unpaired element. You see, XOR is commutative and associative, so <code>2^6^6^2^4^1^4</code> is the same as <code>1^(2^2)^(4^4)^(6^6)</code>; and <code>x^x</code> is always <code>0</code>, so the pairs always cancel either other out. You can just write:</p> <pre><code>int result = 0; for(int i : a) result ^= i; </code></pre> <p>to compute the unpaired element. (To get the <em>index</em> of the unpaired element, you'd then iterate over the array again, looking for <code>result</code>.)</p></li> </ul>
    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