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?
I believe the best solution is;Am I correct?Without a measure for quality/best, I don't see a way to tell. Then, I don't think you need to look at O(n) elements, which your approaches do. (Oh, and please specify whether the elements that are in the 2nd array are in the same order as in the 1st.)