Given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing in the list. Find the missing integer.
Missing number can be found as using XOR.
(PARTIAL_SUM) XOR (MISSING_ELEMENT) = (TOTAL_SUM)
=> (TOTAL_SUM) XOR ( PARTIAL_SUM) = (MISSING_ELEMENT)
because
A XOR B = C => C XOR A = B
To prove this property, just write down the XOR truth table:
A B | A XOR B
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0
Truth table in short : if both the bits are same, result of XOR is false, true otherwise.