Ctrl K

Two Values Without a Pair

Medium
In the array values every number appears exactly twice, except two different numbers that appear once each. Return those two numbers in any order. With a set or a dictionary this is easy. Try for linear time and O(1) extra memory.
Example 1
values=[6, 3, 8, 6, 3, -2] → [-2, 8]

6 and 3 come in pairs, leaving 8 and -2 alone. [8, -2] is also accepted.

Example 2
values=[10, 11] → [11, 10]

There are no pairs at all, so both numbers are single.

Constraints
  • 2 ≤ len(values) ≤ 3·10⁴
  • -2³¹ ≤ values[i] ≤ 2³¹ - 1
  • Exactly two numbers appear once; every other number appears exactly twice
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEvery Topping Combo MediumAll Distinct Card Hands MediumThe Odd One Among Triples MediumSpot the Extra Copy MediumWords With No Shared Letters Medium
loading Python…
Input
values=[6, 3, 8, 6, 3, -2]
Expected
[-2, 8]

Run checks the first 3 tests in your browser. Submit checks all 62 tests on the server, hidden ones included.