Show problems as
Classic problem #260MediumArrayBit Manipulation

Single Number III

Real-world version: Unmatched Sensor Drifts
Tracked in this browser only
Read Single Number III on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A calibration rig reads every probe twice during a sweep and appends the probe's signed drift value to one shared log, so a value that belongs to a fully read probe turns up in the log exactly twice. Two probes worked loose partway through the sweep and were read only once, so their drift values turn up exactly once each. No two probes share a drift value.

Given the sweep log drifts, return the two values that appear exactly once. The two may be returned in either order.

The rig has almost no working memory, so use only a constant amount of extra space beyond the log itself, and read the log a constant number of times.

Examples

Example 1

Input
drifts = [12, -5, 9, -5, 12, 7]
Output
[7, 9]

The values -5 and 12 each show up twice, while 9 and 7 each show up once.

Example 2

Input
drifts = [6, 10, 4, 4]
Output
[6, 10]

Only 4 is logged twice, so the loose probes are the ones that reported 6 and 10.

Example 3

Input
drifts = [-31, 18]
Output
[-31, 18]

The log holds nothing but the two single readings, so both of them are loners.

Constraints

  • 2 <= drifts.length <= 3 * 10^4
  • -2^31 <= drifts[i] <= 2^31 - 1
  • Every value in drifts appears exactly twice, except for exactly two values that each appear exactly once.

The values you return may be in any order.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def unmatched_drifts(drifts: list[int]) -> list[int]:
Java
public int[] unmatchedDrifts(int[] drifts)