Given two integer arrays where second array is duplicate of first array with just 1 element missing. Find the element.
Example:
Input:
Array1 - 9 7 8 5 4 6 2 3 1
Array2 - 2 4 3 9 1 8 5 6
Output:
7
Solution:
var array1 = [9 ,7, 8, 5, 4, 6, 2, 3, 1];
var array2 = [2 ,4 ,3, 9, 1, 8, 5, 6];
function getMissingNumber (array1, array2){
var missingNumber = undefined;
array1.forEach((elm, index)=>{missingNumber = missingNumber^elm^array2[index]})
return missingNumber;
}
getMissingNumber(array1, array2)
Example:
Input:
Array1 - 9 7 8 5 4 6 2 3 1
Array2 - 2 4 3 9 1 8 5 6
Output:
7
Solution:
var array1 = [9 ,7, 8, 5, 4, 6, 2, 3, 1];
var array2 = [2 ,4 ,3, 9, 1, 8, 5, 6];
function getMissingNumber (array1, array2){
var missingNumber = undefined;
array1.forEach((elm, index)=>{missingNumber = missingNumber^elm^array2[index]})
return missingNumber;
}
getMissingNumber(array1, array2)
No comments:
Post a Comment