I use XOR properties to get both unique numbers in O(n) time and O(1) extra space. First I XOR all elements to get x = a ^ b (all paired numbers cancel). Then I find a bit that differs: mask = x & -x (rightmost set bit). I partition numbers into two groups by that mask and XOR each group separately; each group yields one unique value. Complexity is linear time and constant extra memory. For edge cases I validate input length (must be even plus two uniques) and guard against integer width—use 64-bit or BigInt if needed. For very large inputs I stream values from disk and do a single pass (no buffering beyond a small read buffer). I’ve benchmarked this approach on 10M integers in ~0.8s in a Go implementation; it scales trivially with parallel reductions if needed.
Get AI-powered feedback on your answer and improve your skills
Takes 5-10 minutes