You are given an integer array nums in which every value occurs exactly twice, except for one value that occurs only once. Return the value that has no partner.
Aim for a solution that reads the array in a single pass of O(n) time and keeps only O(1) extra memory: no set, no map, no sorted copy.
9 and 0 each show up twice. -4 shows up once, and the pairs may be scattered anywhere in the array.
A one-element array has nothing to pair with, so its only value is the answer.
Constraints
1 <= nums.length <= 3 * 10⁴ (always odd)-3 * 10⁴ <= nums[i] <= 3 * 10⁴Think of an operation where a value combined with itself cancels to nothing, and where the order you combine things in makes no difference. Run it across the whole array and every pair disappears.
Every value but one comes in a pair, and the job is to find the one with no partner. The direct way is to count: how many times does each value appear? The value seen once is the answer. Counting by rereading the array for every value is slow, and counting in a hash map is fast but keeps a table as big as the input.
Time O(n)Space O(1)
The full walkthrough and code are part of Premium
3