
- 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
Find elements which are present in first array and not in second in C++
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
- Related Articles
- Elements present in first array and not in second using STL in C++
- Count elements present in first array but not in second 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++
- Find the first, second and third minimum elements in an array in C++ program
- Return the bases when first array elements are raised to powers from second array in Python
- Find difference between first and second largest array element in Java
- Subtracting array in JavaScript Delete all those elements from the first array that are also included in the second array
- Python Program that Displays which Letters are in the First String but not in the Second
- Find the smallest and second smallest elements in an array in C++
- 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
- Find Array Elements Which are Greater than its Left Elements in Java?

Advertisements