Algorithm: find the missing element in 2 given tables, the second table is duplicated

advertisements

Find the missing element from the given 2 arrays, second array is duplicate.

Example: array 1: [1,2,3,4,5,6,7] array 2: [1,3,4,5,6,7]

I read about using hashmaps and other complex approaches, but I believe the best solution is:

1) Add all elements of array1 and array2 separately, such that we have sum1 and sum2, then the answer is |sum2 - sum1|

2) XOR all elements of array1 and array2 separately, such that we have xor1 and xor2. Here xor1 is always from the complete array. The missing element will be xor2 XOR xor1 (from XOR applications http://www.codeproject.com/Articles/2983/XOR-tricks-for-RAID-data-protection)

Edit: arrays not sorted

Am I correct?


Yes, your algorithm is correct, assuming the array contains integers. The proof easily follows from the fact that x^x=0 if and only if x=x. It even works in the case the array contains duplicates.

Proof sketch. Let A be an array of n integers and let B be a copy of A with one element removed. Define xorA := A[0] ^ A[1] ^ ... ^ A[n-1] and xorB := B[0] ^ ... ^ B[n-2]. Since each element, except the missing one, appears even number of times, xorA ^ xorB will equal the missing element which appears odd number of times---all others cancel out because x^x=0.