Locate the Lone Value in a Sorted Paired List
Heaps, Sorting & SearchAuto-graded · 18 test casesLast updated
Use parity-based binary search on a sorted array of duplicated values to find the one value without a partner in logarithmic time and constant extra space.
Lone Value in a Sorted Paired List
You receive a list of integers values that is sorted in non-decreasing order. Every number in it occurs exactly two times, with a single exception: one number occurs only once.
Return that number that has no partner.
Your algorithm must run in O(log n) time and use O(1) extra space, where n is the length of the list.
Input
values: a sorted list of integers whose length is odd, in which exactly one value appears once and every other value appears exactly twice.
Output
- A single integer: the value that appears exactly once.
Notes
- A list with a single element is valid, and that element is the answer.
- The lone value may be at the very start or the very end of the list.
Constraints
1 <= len(values) <= 10^50 <= values[i] <= 10^5valuesis sorted in non-decreasing order.- Exactly one value appears once; all other values appear exactly twice.
Examples
Example 1
Input: find_lone_value([1, 1, 2, 3, 3, 4, 4, 8, 8])
Output: 2
Example 2
Input: find_lone_value([3, 3, 7, 7, 10, 11, 11])
Output: 10
Hints
Hint 1
Equal values are adjacent because the list is sorted. Think about the index positions where each pair begins, rather than the values themselves.
Hint 2
Before the lone value, each pair starts at an even index. After it, every pair starts at an odd index. This gives you a condition that flips exactly once.
Hint 3
Binary search over even indices only: if values[mid] equals values[mid + 1] (with mid even), the lone value is to the right of that pair; otherwise it is at mid or to its left.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Because the list is sorted, equal values sit next to each other. The key observation concerns index parity.
- Left of the lone value, every pair occupies indices
(even, even + 1). - At and after the lone value, the pairing shifts by one, so pairs occupy
(odd, odd + 1).
The lone value itself is always at an even index, since everything before it consists of complete pairs.
So for an even index i, the test values[i] == values[i + 1] is true for all even i before the lone value and false from the lone value onward. A condition that flips exactly once is ideal for binary search.
Algorithm
- Set
lo = 0andhi = len(values) - 1. The length is odd, so both are even. - While
lo < hi, takemid = (lo + hi) // 2and, if it is odd, subtract one so it is even. - If
values[mid] == values[mid + 1], the pairing is intact up to here, so the lone value lies to the right:lo = mid + 2. - Otherwise the lone value is at
midor to its left:hi = mid. - When
lo == hi, returnvalues[lo].
Why it is correct
The invariant is that lo and hi are even and lo <= u <= hi, where u is the index of the lone value.
- Moving
lotomid + 2is only done when the pair atmidis intact, which meansu > mid + 1, sou >= mid + 2(anduis even). - Moving
hitomidis only done when the pair atmidis broken, which meansu <= mid. - Since
midis even andlo <= mid < hi, we havemid + 1 <= hi - 1, so the accessvalues[mid + 1]is always in range. - Each iteration strictly shrinks the window, so the loop ends with
lo == hi == u.
Edge cases: a single-element list skips the loop and returns that element; a lone value at the start makes the pair check fail everywhere, collapsing hi to 0; a lone value at the end makes every check succeed, pushing lo to the last index.
Complexity
- Time:
O(log n), as the window roughly halves each iteration. - Space:
O(1).
Reference solution
from typing import List
def find_lone_value(values: List[int]) -> int:
lo, hi = 0, len(values) - 1
while lo < hi:
mid = (lo + hi) // 2
if mid % 2 == 1:
mid -= 1
if values[mid] == values[mid + 1]:
lo = mid + 2
else:
hi = mid
return values[lo]