Skip to content
BytePatterns

The Missing Value

EasyBit Manipulation#xor-cancel#single-pass~20m

Problem

A list holds n distinct values drawn from 0 up to n, in any order, so exactly one value of that range is absent. Find it in one pass using constant extra space, which rules out sorting and rules out a set of everything seen.

Examples

Input:  nums = [3, 0, 1]
Output: 2
Input:  nums = [9, 6, 4, 2, 3, 5, 7, 0, 1]
Output: 8
Input:  nums = [0]
Output: 1
Why:    edge case, the missing value can be n itself

Hints

0 / 3

Stuck on the idea rather than the code? XOR Tricks covers it.