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

Updated on: 20-Jan-2020

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements