mediumBit ManipulationPure DSA~30 min
Identify Two Unique Faulty Sensors
Every sensor reports a paired heartbeat ID twice, except two faulty sensors that each report exactly once. Given the multiset of IDs, recover the two unique IDs using constant space.
Problem
Given an integer array nums in which exactly two elements appear only once and all other elements appear exactly twice, return the two elements that appear only once. The answer may be in any order. Use linear time and constant extra space.
Input
An integer array nums of length [2, 3*10^4]; exactly two elements appear once, the rest twice.
Output
An array of the two unique integers (any order).
Constraints
- 2 <= nums.length <= 3 * 10^4
- Each integer fits in a 32-bit signed int.
- Exactly two elements appear once; every other appears twice.
Examples
Example 1
Input
nums = [1,2,1,3,2,5]
Output
[3,5]
3 and 5 are the unpaired values.
Example 2
Input
nums = [-1,0]
Output
[-1,0]
Both appear once.