# Find elements which are present in first array and not in second in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

Suppose we have two arrays A and B. There are few elements. We have to find those elements that are present in set A, but not in set B. If we think that situation, and consider A and B as set, then this is basically set division operation. The set difference between A and B will return those elements.

## Example

#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
void setDiffResults(int A[], int B[], int An, int Bn) {
sort(A, A + An);
sort(B, B + Bn);
vector<int> res(An);
vector<int>::iterator it;
vector<int>::iterator it_res = set_difference(A, A + An, B , B + Bn, res.begin());
cout << "Elements are: ";
for(it = res.begin(); it < it_res; ++it){
cout << *it << " ";
}
}
int main() {
int A[] = {9, 4, 5, 3, 1, 7, 6};
int B[] = {9, 3, 5};
int An = 7, Bn = 3;
setDiffResults(A, B, An, Bn);
}

## Output

Elements are: 1 4 6 7
Updated on 01-Nov-2019 07:19:55