iq.lab
Python starts when a code cell comes near or you run one
easyBits and math target 15 min

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.

Example 1
Inputnums = [5, 3, 5]Output3

5 appears twice. 3 appears once.

Example 2
Inputnums = [2, 7, 9, 7, 2]Output9

The copies of 2 sit at positions 0 and 4, the copies of 7 at positions 1 and 3. Only 9 appears once.

Example 3
Inputnums = [8]Output8

A list of one item: that item is the one without a twin.

Constraints
  • 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.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.