
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find lost element from a duplicated array in C++
Concept
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.
Input
arr1[] = {2, 5, 6, 8, 10} arr2[] = {5, 6, 8, 10}
Output
2
2 is missing from second array.
Input
arr1[] = {3, 4, 5, 6} arr2[] = {3, 4, 5, 6, 7}
Output
7
7 is missing from first array.
Method
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.
Example
// 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; }
Output
Missing Element is 2
- Related Articles
- Find lost element from a duplicated array in Python
- Remove duplicated elements of associative array in PHP
- 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#?
- How to Find a Lost Cell Phone?
- Find largest element from array without using conditional operator in C++
- C# Program to find the smallest element from an array
- C# Program to find the largest element from an array
- Extract a particular element from a nested array in MongoDB
- Program to find maximum XOR with an element from array in Python
- Remove element from array referencing spreaded array in JavaScript
- Find a peak element in a 2D array in C++
- Removing an array element from a MongoDB collection
- Unset an attribute from a single array element in MongoDB?
- How to remove a specific element from array in MongoDB?

Advertisements