Meta VO Interview Question: Find the Unique Element in an Array Where Every Other Element Appears Twice

16 Views
No Comments

Find the unique element in an array where all other elements appear twice except one.

Examples:

[1, 1, 4, 4, 7, 8, 8, 29, 29] -> 7
[1, 2, 2, 3, 3] -> 1

The key idea is to identify the one number that appears only once while every other number appears exactly twice. A straightforward solution uses a hash map to count frequencies, then returns the value with count 1. A more optimal solution uses XOR: equal numbers cancel each other out, and XOR with 0 preserves the value, so XOR-ing all elements leaves the unique element. This is a classic interview problem that checks understanding of bitwise operations and O(n) time, O(1) extra space solutions.

END
 0