How to Check If a Set Contains an Element in C++?
Last Updated :
21 Oct, 2024
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++:
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++
// 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;
}
OutputThe 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++
// 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;
}
OutputThe 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++
// 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 Present
Time 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++
// 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;
}
OutputThe Value 45 is Present
Time Complexity: O(n), where n is the number of element in the set container
Auxiliary Space: O(1)
Similar Reads
How to Check if a Vector Contains a Given Element in C++? In C++, a vector is a dynamic array that provides a resizable and efficient way to store elements. Vectors store their elements in contiguous memory locations, similar to arrays, which allows for efficient access and iteration.In this article, we will learn the different methods to check whether the
3 min read
How to Access an Element in Set in C++? In C++ STL (Standard Template Library), the set container represents a collection of unique, sorted elements. In this article, we will see how to an element in a set in C++ using iterators. Example Input: mySet = {1, 8, 99, 444, 521 } Output: mySet Value at Index 2: 99Access an Element in a Set in C
2 min read
How to Check if a Set is Empty in C++? In C++, a set is an associative container that stores unique elements in a sorted order. In this article, we'll explore different approaches to check if a set is empty in C++ STL. Check if a Set is Empty or Not in C++To check if a std::set is empty in C++, we can use the std::set::empty() function.
2 min read
How to Check if Map Contains a Specific Key in C++? In C++, std::map is an STL container that stores key-value pairs in a sorted order based on the keys. In this article, we will learn how to check if an element with the given key exists in a map or not in C++.ExamplesInput: m = {{5, "apple"}, {4, "kiwi"}, {6, "cherry"}}, k = 4Output: Key found.Expla
4 min read
How to Delete an Element from a Set in C++? A set in C++ is a container that stores unique elements in a sorted order. In this article, we will learn how to delete a specific element from a set. Example: Input: mySet = {5, 2, 8, 1, 4} Element to delete: 2 Output: mySet = {5, 1, 8, 4}Delete an Element from a Set in C++To delete a specific elem
2 min read