Find the Unique Element in an Array of Duplicates
viaGlassdoor
Problem: Given an array where every element appears exactly twice except for one element that appears exactly once, find that unique element.
Constraints: Ideally O(n) time and O(1) extra space.
Example: [4,1,2,1,2] -> unique element is 4.
Approach: XOR all elements together. Since a XOR a = 0 and XOR is commutative/associative, all paired duplicates cancel out to 0, leaving only the unique element. O(n) time, O(1) space.
asked …