
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Find Common Elements from Two Arrays in C++
The usage of arrays, and data structures, allows for the storage of homogeneous (same) data over several memory locations. The main advantage of using arrays is that we can access them from anywhere we want by using the index argument. The fact that data must be added and removed sequentially transforms this data structure into a linear one. To retrieve an element from an array, all we need to do is use the index or position number for that element inside the square bracket. In this article, we will take two arrays and find only the common elements present in both arrays using C++.
Understanding the concept with examples
Given first array A = [10, 14, 65, 85, 96, 12, 35, 74, 69] Given second array B = [23, 65, 89, 96, 12, 37, 71, 69] The common elements in arrays A and B are [65, 96, 12, 69]
In the first array, there are nine elements and in the second array, there are eight elements. So the arrays may not be of the same size. Our task is to find the common elements between these two arrays. Here we will see a few techniques to solve this problem.
Naïve Solution
The first and most common solution is looping through each element from the first array and for each entry of the first array search into the second array. This solution is not much efficient, but a simpler solution. Let us see the algorithm and corresponding implementation.
Algorithm
Take two arrays A and B as input
Define another array D to hold all duplicate elements
-
for each element e1 in A, do
-
for each element e2 in B, do
-
if e1 = e2, then
insert e1 into D
end if
-
end for
-
end for
return D
Example
#include <iostream> # define Z 50 using namespace std; void displayArr(int arr[], int n){ for( int i = 0; i < n; i++ ){ cout << arr[ i ] << ", "; } cout << endl; } void findCommonElement( int A[], int n, int B[], int m, int D[], int &k ) { k = 0; for( int i = 0; i < n; i++ ) { for( int j = 0; j < m; j++ ) { if( A[ i ] == B[ j ] ) { D[ k ] = A[ i ]; k = k + 1; } } } } int main() { int A[ Z ] = { 10, 14, 65, 85, 96, 12, 35, 74, 69 }; int n = 9; int B[ Z ] = { 23, 65, 89, 96, 12, 37, 71, 69 }; int m = 8; int D[ Z ]; int k = 0; cout << "Given first array A: "; displayArr( A, n ); cout << "Given second array B: "; displayArr( B, m ); findCommonElement( A, n, B, m, D, k ); cout << "The common elements are: "; displayArr( D, k ); }
Output
Given first array A: 10, 14, 65, 85, 96, 12, 35, 74, 69, Given second array B: 23, 65, 89, 96, 12, 37, 71, 69, The common elements are: 65, 96, 12, 69,
Using Vectors and set_intersection() function
The set_intersection() functions come with C++ STL. Using this method the common elements are returned as an iterator object. But to use this function, we must sort the arrays in ascending order. Let us see the algorithm and C++ implementation code.
Algorithm
Take two arrays A and B as input
Define another array D to hold all duplicate elements
Create an iterator for duplicate elements array
use set_intersection() method with A and B array and store inside D Array
return D
Example
#include <iostream> #include <algorithm> #include <vector> # define Z 50 using namespace std; void displayArr( vector<int> v ){ for( int i = 0; i < v.size() ; i++ ){ cout << v[ i ] << ", "; } cout << endl; } vector<int> findCommonElement( vector<int> A, vector<int> B ) { sort( A.begin(), A.end() ); sort( B.begin(), B.end() ); vector<int> duplicates; vector<int> D( A.size() + B.size() ); vector<int>::iterator Dit, st; Dit = set_intersection( A.begin(), A.end(), B.begin(), B.end(), D.begin() ); for( st = D.begin(); st != Dit; ++st ) duplicates.push_back( *st ) ; return duplicates; } int main() { vector<int> A = { 10, 14, 65, 85, 96, 12, 35, 74, 69 }; vector<int> B = { 23, 65, 89, 96, 12, 37, 71, 69 }; vector<int> D; cout << "Given first array A: "; displayArr( A ); cout << "Given second array B: "; displayArr( B ); D = findCommonElement( A, B ); cout << "The common elements are: "; displayArr( D ); }
Output
Given first array A: 10, 14, 65, 85, 96, 12, 35, 74, 69, Given second array B: 23, 65, 89, 96, 12, 37, 71, 69, The common elements are: 12, 65, 69, 96,
Conclusion
In this article, we have seen two methods to find the common elements from the set of elements or two arrays. The first naïve solution takes two static arrays and finds the common elements by simply scanning through each element one by one. This solution takes O(n.m) time where n is the size of the first array and m is the size of the second array. The next method uses the C++ STL-based set_intersection() method. In this method, we need to use sorted vectors. After that, the method returns an iterator object for the common elements iterator. We can create a vector from it and return it.