My first instinct was XOR, which gets you there in O(n).
First, clarify that the O(log n) requirement implies the array is sorted, as binary search is otherwise impossible. Then, explain that in a sorted array, the unique element can be found by comparing pairs at even indices; use binary search to discard halves where all elements appear in pairs.
Pro tip: Always state your assumptions explicitly—here, that the array is sorted. If the interviewer confirms, proceed; if not, discuss the impossibility of O(log n) and offer an O(n) XOR solution as a fallback.
Ask if the array is sorted. If not, explain that O(log n) is impossible and propose an O(n) solution using XOR.
In a sorted array, all elements before the unique element appear in pairs at indices (0,1), (2,3), etc. After the unique element, the pairing shifts.
Use binary search on even indices. If the element at mid equals the next element, the unique is to the right; otherwise, it's to the left.
Consider arrays of length 1, unique at start or end, and ensure mid is always even to maintain pair alignment.
Explain that each step halves the search space, giving O(log n) time and O(1) space.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.