The value without a twin
The list of integers nums was built from pairs. Each pair is two copies of one value, and no two pairs share a value. Then one extra value, different from every paired value, was added once. Return that extra value.
The list can be in any order: the two copies of a value need not be next to each other, and the extra value can sit anywhere.
Counting the copies of each value with nums.count is correct but fails the speed test. A dict of counts passes it, at the price of O(n) extra space. Aim for O(n) time and O(1) extra space: a few variables, however long the list. The tests can check only the time.
nums = [5, 3, 5]Output35 appears twice. 3 appears once.
nums = [2, 7, 9, 7, 2]Output9The copies of 2 sit at positions 0 and 4, the copies of 7 at positions 1 and 3. Only 9 appears once.
nums = [8]Output8A list of one item: that item is the one without a twin.
1 ≤ len(nums) ≤ 50,001, and the length is odd.
-109 ≤ nums[i] ≤ 109
Each value appears exactly twice, except the answer, which appears once.
Plan it first
Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.