Find the Missing and the Repeated Number via XOR

Reported in interviews at Akuna Capital

Problem statement

An array holds the numbers $1$ to $n$ except that exactly one value is missing and, in its place, another value appears twice. Find both the missing and the repeated number. Solve it with XOR, write your own tests, then discuss alternative arithmetic methods.

Example

find_missing_repeated([1, 1], 2) -> [2, 1]
Show hint

Which bitwise operation makes every value that appears in both the array and the range 1..n disappear?

Relevant roles: ML Engineer, Forward Deployed Engineer and AI Engineer.

Related free problems