Elements present in first array and not in second using STL in C++
Last Updated :
11 Jul, 2025
Given two arrays, the task is that we find numbers which are present in first array, but not present in the second array, using STL in C++ Examples:
Input: a[] = {1, 2, 3, 4, 5, 10}, b[] = {2, 3, 1, 0, 5}
Output: 4 10
Input:a[] = {4, 3, 5, 9, 11}, b[] = {4, 9, 3, 11, 10};
Output: 5
Approach: In STL, the set_difference() method can be used to find the 'A-B' where A is the first array and B is the second array. Syntax:
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result);
Below is the implementation of the above approach:
CPP
// C++ simple program to
// find elements which are
// not present in second array
#include <bits/stdc++.h>
using namespace std;
// Function for finding
// elements which are there
// in a[] but not in b[].
void findMissing(int a[], int b[],
int n, int m)
{
// Declare a vector to store the result
vector<int> v(n + m);
// And an iterator to traverse the vector
vector<int>::iterator it;
// Sort the given arrays
sort(a, a + n);
sort(b, b + m);
// Find the elements in a[]
// which are not in b[]
it = set_difference(a, a + n, b, b + m, v.begin());
// Now resize the vector to the existing count
v.resize(it - v.begin());
// Print the results
cout << "The elements in a[]"
<< " which are not in b[]:\n";
for (it = v.begin(); it != v.end(); ++it)
cout << *it << " ";
cout << endl;
}
// Driver code
int main()
{
int a[] = { 1, 2, 6, 3, 4, 5 };
int b[] = { 2, 4, 3, 1, 0 };
int n = sizeof(a) / sizeof(a[0]);
int m = sizeof(b) / sizeof(b[1]);
findMissing(a, b, n, m);
return 0;
}
Output:The elements in a[] which are not in b[]:
5 6
Time Complexity: O(nlogn + mlogm), used for sorting the given arrays
Auxiliary Space: O(n+m), extra space of size (n+m) used for creating vector
Similar Reads
Remove duplicate elements in an Array using STL in C++ Given an array, the task is to remove the duplicate elements from the array using STL in C++ Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} Approach: This can be done using set in standard template library. Set
2 min read
How to Insert a Range of Elements in a Set in C++ STL? Prerequisites: Set in C++ Sets in C++ are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific sorted order i.e. either ascending or descending. Syntax: set<datatype> set_name; Some Basic Func
2 min read
How to copy elements of an Array in a Vector in C++ An Array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled
6 min read
Remove duplicates from an unsorted array using STL in C++ Given an unsorted array, the task is to remove the duplicate elements from the array using STL in C++ Examples: Input: arr[] = {1, 2, 5, 1, 7, 2, 4, 2} Output: arr[] = {1, 2, 4, 5, 7} Input: arr[] = {1, 2, 4, 3, 5, 4, 4, 2, 5} Output: arr[] = {1, 2, 3, 4, 5} Approach: The duplicates of the array can
2 min read
How to sort an Array using STL in C++? Given an array arr[], sort this array using STL in C++. Example: Input: arr[] = {1, 45, 54, 71, 76, 12} Output: {1, 12, 45, 54, 71, 76} Input: arr[] = {1, 7, 5, 4, 6, 12} Output: {1, 4, 5, 6, 7, 12} Approach: Sorting can be done with the help of sort() function provided in STL. Syntax: sort(arr, arr
1 min read
Different Ways to Insert Elements in Set in C++ STL Prerequisites: Set in C++ The C++ Standard Template Library offers containers called Sets. It functions essentially in the same ways as a binary search tree and is used to store different elements in increasing/decreasing order. There are different methods to insert elements in the set as mentioned
3 min read