find missing number in array

🏠

Given an array [0...n] with a missing number, write a program to efficiently find the missing number.

The solution is truly beautiful, and consists of:

functional style

1 from functools import reduce
2 from operator import ixor
3 
4 find_missing = lambda arr: reduce(ixor, arr) ^ reduce(ixor, range(len(arr) + 1))
5 find_missing([0, 1, 2, 3, 4, 5, 6, 7, 8, 10])

Result:

1 9