Show problems as
Classic problem #137MediumArrayBit Manipulation

Single Number II

Real-world version: Once-Logged Drift Reading
Tracked in this browser only
Read Single Number II on LeetCode(opens in a new tab)

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

A calibration bench mirrors every drift reading it takes into three separate journals, so the merged dump carries three copies of each reading the bench produced. One reading was keyed in by hand before mirroring was switched on, and that reading sits in the dump on its own.

The merged dump arrives as the integer array readings. Return the reading that occurs only once. Drift may run either side of zero, so a reading can be negative.

Work in time proportional to the length of readings, and hold only a fixed number of extra values while you do it.

Examples

Example 1

Input
readings = [4, 7, 4, 4, 9, 7, 7, 12, 12, 12]
Output
9

Counting copies: 4 appears three times, 7 appears three times and 12 appears three times, while 9 appears once.

Example 2

Input
readings = [-5, 8, 8, 8]
Output
-5

The dump holds three copies of 8 and a single -5, and the hand-keyed reading is the negative one.

Example 3

Input
readings = [10, 10, 1, 10, 1, 1, -6]
Output
-6

The dump carries three copies of 10 and three copies of 1, and -6 is the reading with a single copy.

Constraints

  • 1 <= readings.length <= 3 * 10^4
  • -2^31 <= readings[i] <= 2^31 - 1
  • Every reading in readings occurs exactly three times, except one reading that occurs exactly once

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 once_logged_reading(readings: list[int]) -> int:
Java
public int onceLoggedReading(int[] readings)