Single Number II
Real-world version: Once-Logged Drift ReadingThe 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.
def once_logged_reading(readings: list[int]) -> int:public int onceLoggedReading(int[] readings)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's