All problems
0486MediumArrayBit Manipulation

Unmatched Sensor Drifts

Tracked in this browser only
Write code

Trains the technique from

LeetCode 260Single Number III

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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