In C++, the std::set container stores the unique elements in the sorted order. In this article, we will learn how to check if the set contains a given element or not.
Examples
Input: s = {1, 3, 5, 7, 9}, x = 5
Output: Element found
Explanation: The element 5 is present in the set.Input: s = {10, 20, 30, 40}, x = 25
Output: Element not found
Explanation: The element 25 is not present in the set.
Following are the 4 different methods to check if a set contains an element or not in C++:
Table of Content
Using std::set::find() Method
C++ STL provides the std::set::find() method that searches the set for a given element and returns an iterator to the element if it is found. If it is not found, std::set::end() is returned. It is the member function of std::set() container so we can directly use it with any set.
Example
// C++ program to check if a set contains
// an element using set::find() method
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> s = {1, 3, 5, 7, 9};
int x = 5;
// Check if the element is found
if (s.find(x) != s.end()) {
cout << "Element found";
} else {
cout << "Element not found";
}
return 0;
}
Output
The Value 45 is Present
Time Complexity: O(log n), where n is the number of elements in the set.
Auxiliary Space: O(1)
Using std::set::count() Method
We can also use std::set::count() method to check whether the std::set contains an element or not. If the element present, it returns 1, otherwise, returns 0. It is also a member function of std::set container.
Example
// C++ program to check if a set contains
// an element using set::count() method
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> s = {1, 3, 5, 7, 9};
int x = 4;
// Check if the element exists using count()
if (s.count(x) > 0) {
cout << "Element found";
} else {
cout << "Element not found";
}
return 0;
}
Output
The Value 45 is Present
Time Complexity: O(log n), where n is the number of element in the set container.
Auxiliary Space: O(1)
Using set::contains() Method (C++ 20)
Since C++ 2, we can also use std::set::contains() method to check whether the std::set contains an element or not. If the element present, it returns true, otherwise, returns false.
Example
// C++ program to check if a set contains
// an element using set::contains()
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> s = {1, 3, 5, 7, 9};
int x = 7;
// Check if element exists using
// set::contains()
if (s.contains(x)) {
cout << "Element found";
} else {
cout << "Element not found";
}
return 0;
}
Output
The Value 45 is PresentTime Complexity: O(log n), where n is the number of element in the set container
Auxiliary Space: O(1)
Manually Using Loop
Another way to check if a set contains an element is by manually iterating through the std::set container using a loop with iterators. By comparing each element with the value inside if statement, you can check whether the element is present in the set.
Example
// C++ program to check if a set contains
// an element using a manual loop
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> s = {1, 3, 5, 7, 9};
int x = 3;
bool f = false;
// Manually iterate over set to find x
for (int i : s) {
if (i == x) {
f = true;
break;
}
}
// Check if element exists
if (f) {
cout << "Element found";
} else {
cout << "Element not found";
}
return 0;
}
Output
The Value 45 is Present
Time Complexity: O(n), where n is the number of element in the set container
Auxiliary Space: O(1)