- Related Questions & Answers
- Find missing number in a sequence in C#
- Find the missing number in Arithmetic Progression in C++
- Find the missing number in Geometric Progression in C++
- How to copy a section of an array into another array in C#?
- Array Copy in C#
- Program to find kth missing positive number in an array in Python
- Find N digits number which is divisible by D in C++
- How to copy a section of one Array to another in C#?
- How can we copy one array from another in Java
- Missing Element in Sorted Array in C++
- Find missing element in a sorted array of consecutive numbers in C++
- Missing Number in Python
- How to find the missing number in a given Array from number 1 to n in Java?
- Missing Number In Arithmetic Progression using C++
- Find length of longest subsequence of one string which is substring of another string in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose, we have two arrays A and B, the array A has n elements. The second array B has all the elements of A, but they are shuffled and one element is removed. We have to find the missing elements. So if A = [4, 8, 1, 3, 7], and B = [7, 4, 3, 1], the output is 8.

This can be solved using XOR trick. The combined occurrence of each element is twice, one in A, and other in B, except one element which only has a single occurrence in A. As we know that x XOR x = 0, so if we perform XOR in elements of both the arrays. The result will be missing number.

#include<iostream> using namespace std; int FindMissingElement(int A[], int B[], int n) { int min_element = 0; for (int i = 0; i < n; i++) min_element = min_element ^ A[i]; for (int i = 0; i < n - 1; i++) min_element = min_element ^ B[i]; return min_element; } int main() { int A[] = {4, 8, 1, 3, 7}; int B[] = {7, 4, 3, 1}; int n = sizeof(A) / sizeof(A[0]); cout << "Missing element: " << FindMissingElement(A, B, n); }

Missing element: 8

Advertisements