- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

With respect of given two arrays which are duplicates of each other except one element, that means one element from one of the array is missing, our task to determine that missing element.

arr1[] = {2, 5, 6, 8, 10} arr2[] = {5, 6, 8, 10}

2

2 is missing from second array.

arr1[] = {3, 4, 5, 6} arr2[] = {3, 4, 5, 6, 7}

7

7 is missing from first array.

Here, we apply one simple solution where we iterate over arrays and verify element by element and mark the missing element when an unmatched is detected. But the drawback of this solution is that it requires linear time over size of array.

We can implement another efficient solution which is based on binary search approach. We follow the below algorithm which is explained step by step −

- Begin binary search in bigger array and get mid as (low + high) / 2
- It has been seen that if value from both array is same then missing element must be in right part so mark low as mid
- Else mark high as mid as missing element must be in left part of bigger array if mid elements is not same.
- We have to handle special case separately because for single element and zero element array, single element itself will be the missing element.

It has been observed that if first element itself is not equal then that element will be the missing element.

// C++ program to find missing element from same // arrays (except one missing element) #include <bits/stdc++.h> using namespace std; // Shows function to determine missing element based on binary // search approach. arrA[] is of larger size and // Q is size of it. arrA[] and arrB[] are assumed // to be in same order. int findMissingUtil(int arrA[], int arrB[], int Q){ // Considers special case, for only element which is // missing in second array if (Q == 1) return arrA[0]; // Considers special case, for first element missing if (arrA[0] != arrB[0]) return arrA[0]; // Used to initialize current corner points int low = 0, high = Q - 1; // Iterate until low < high while (low < high){ int mid = (low + high) / 2; // It has been observed that if element at mid indices are equal // then go to right subarray if (arrA[mid] == arrB[mid]) low = mid; else high = mid; // So if low, high becomes contiguous, break if (low == high - 1) break; } // Now missing element will be at high index of // bigger array return arrA[high]; } // So this function mainly does basic error checking // and calls findMissingUtil void findMissing(int arrA[], int arrB[], int P, int Q){ if (Q == P-1) cout << "Missing Element is " << findMissingUtil(arrA, arrB, P) << endl; else if (P == Q-1) cout << "Missing Element is " << findMissingUtil(arrB, arrA, Q) << endl; else cout << "Invalid Input"; } // Driver Code int main(){ int arrA[] = {2, 5, 6, 8, 10}; int arrB[] = {5, 6, 8, 10}; int P = sizeof(arrA) / sizeof(int); int Q = sizeof(arrB) / sizeof(int); findMissing(arrA, arrB, P, Q); return 0; }

Missing Element is 2

- Related Questions & Answers
- Find lost element from a duplicated array in Python
- Find the Smallest element from a string array in JavaScript
- How to remove duplicates from the sorted array and return the non-duplicated array using C#?
- C# Program to find the largest element from an array
- C# Program to find the smallest element from an array
- Find largest element from array without using conditional operator in C++
- Find a peak element in a 2D array in C++
- Remove duplicated elements of associative array in PHP
- How to delete/remove an element from a C# array?
- C# Program to find the smallest element from an array using Lambda Expressions
- C# Program to find the largest element from an array using Lambda Expressions
- Find the K-th minimum element from an array concatenated M times in C++
- Find a peak element in C++
- Find missing element in a sorted array of consecutive numbers in C++
- Find Surpasser Count of each element in array in C++

Advertisements