
- 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
Elements present in first array and not in second using STL in C++
We have two arrays, the task is to compare the two arrays and find the numbers present in first array but not in the second array, using Standard Template Library (STL) in C++.
Example
Input: array1[ ] = {1,2,3,4,5,7} array2[ ] = {2,3,4,5,6,8} Output: 1, 7 Input: array1[ ] = {1,20,33,45,67} array2[ ] = {1,12,13,114,15,13} Output: 20,33,45,67
Approach used in the below program is as follows −
- In this program we want to find the elements which are present in the first array but not present in the second array.
- In order to do this we firstly initializes the two variables. Now we will create a function named “find” to find elements which are there in array 1 and not in array 2.
- In the function we would declare a vector (Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted.) to store the result and we will also declare an iterator to traverse the vector.
- Now we would sort the arrays and find the missing elements using set_difference( ) method, and resize the vector according to the result and store the values and then afterwards print the solution
In Standard Template Library (STL), we can use the set_difference( ) method to find “array1-array2”. The difference of two sets is formed by the elements that are present in the first set, but not in the second one. The elements copied by the function come always from the first range, in the same order. The elements in the both the ranges shall already be ordered.
Syntax
Syntax for set_difference() is −
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result);
Algorithm
Start Step 1-> Create function for finding missing elements void find(int array1[], int array2[], int x, int y) Declare a vector which stores the result as vector<int> v(x + y) Declare an iterator traverse the vector as vector<int>::iterator it Sort the arrays sort array1 and sort array2 End Find the missing elements diff = set_difference(array1, array1 + x, array2, array2 + y, v.begin()) resize the vector to the existing count v.resize(diff - v.begin()) print the elements present in array1[] and not in array2[] for (diff = v.begin() diff != v.end() ++diff Print *diff End Step 2-> In main() Declare array as int array1and int array2 Declare variable x and y to calculate the size of array1 and array 2 as int x = size of array1 and int y = size of array2 Call the function as find(array1, array2, x, y)
Example
#include <bits/stdc++.h> using namespace std; int main() { int array1[] = { 1, 2, 3, 4, 5, 7 }; int array2[] = { 2, 3, 4, 5, 6, 8 }; int x = sizeof(array1) / sizeof(array1[0]); int y = sizeof(array2) / sizeof(array2[1]); find(array1, array2, x, y); return 0; } // Creating function named “find” for finding missing elements void find(int array1[], int array2[], int x, int y) { // Declaring a vector which stores the result vector<int> v(x + y); // Declaring an iterator traverse the vector vector<int>::iterator it; // Sorting the arrays sort(array1, array1 + x); sort(array2, array2 + y); // Finding the missing elements diff = set_difference(array1, array1 + x, array2, array2 + y, v.begin()); //resizing the vector to the existing count v.resize(diff - v.begin()); cout << "The elements present in array1[] and not in array2[]:”; for (diff = v.begin(); diff != v.end(); ++diff) cout << *diff << " "; cout << endl; }
Output
If we run the above code it will generate the following output −
The elements present in array1[] and not in array2[]: 1,7
- Related Articles
- Count elements present in first array but not in second in C++
- Find elements which are present in first array and not in second in C++
- Find the first, second and third minimum elements in an array in C++
- Delete elements in first string which are not in second string in JavaScript
- Find the first, second and third minimum elements in an array in C++ program
- Sort the second array according to the elements of the first array in JavaScript
- Difference between first and the second array in JavaScript
- Set the first array elements raised to powers from second array element-wise in Numpy
- Maximize first array over second in JavaScript
- Product of maximum in first array and minimum in second in C
- Return the bases when first array elements are raised to powers from second array in Python
- Subtracting array in JavaScript Delete all those elements from the first array that are also included in the second array
- Find elements of an Array which are Odd and Even using STL in C++
- Find difference between first and second largest array element in Java
- Find the smallest and second smallest elements in an array in C++

Advertisements