How to Remove All Occurrences of an Element from a Set in C++? Last Updated : 01 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, a set is an associative container that stores unique elements in a sorted order. In this article, we will learn how to remove all occurrences of a specific element from a set in C++. Example: Input:mySet = {1,2,3,4,5};target= 2Output:After Deletion: 1 3 4 5Delete All Occurrences of an Element from a Set in C++To delete all occurrences of a given element from a set first, search for the element to be removed using the std::set::find function if found erase the occurrence of that element from the set using the std::set::erase function. Note: The set contains only unique elements, so there will be only one occurrence of an element in the set. C++ Program to Remove All Occurrences of an Element from SetThe below program demonstrates how we can remove all occurrences of a specific element from the set in C++ STL. C++ // C++ program to remove all occurences of a specific // element from set #include <algorithm> #include <iostream> #include <set> using namespace std; int main() { // creating a set set<int> mySet = { 1, 2, 3, 2, 4, 5, 2 }; // Printing the initial set cout << "Before deletion: "; for (int elem : mySet) { cout << elem << " "; } cout << endl; int target = 2; // Element to remove // Find the first occurrence of the element to remove auto it = mySet.find(target); // Erase all occurrences of the element while (it != mySet.end()) { mySet.erase(it); // Erase the element it = mySet.find(target); // Find the next occurrence } // Print the set after deletion cout << "After Deletion: "; for (int elem : mySet) { cout << elem << " "; } cout << endl; return 0; } OutputBefore deletion: 1 2 3 4 5 After Deletion: 1 3 4 5 Time Complexity: O(n log(n)), here n is the number of elements in the set. Auxiliary Space: O(1) Note: Here, we're assuming that there is only single occurence of each element in a set. Comment More infoAdvertise with us Next Article How to Remove All Occurrences of an Element from a Set in C++? S susobhanakhuli Follow Improve Article Tags : C++ Programs C++ STL cpp-set CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Remove All Occurrences of an Element from List in C++? In C++, Lists are sequence containers that allow non-contiguous memory allocation. In this article, we will learn how to remove an element from a list in C++. Example Input: myList = {100, 78, 120, 12, 56, 78, 78}target = 78Output:// Removed element 78 from the list{ 100, 120, 12, 56}Remove an Eleme 2 min read How to Remove All Occurrences of an Element from Deque in C++? In C++, a deque (double-ended queue) is a container that allows insertion and deletion at both its beginning and end. In this article, we will learn how to remove all occurrences of a specific element from a deque in C++. Example: Input: deque = {1, 2, 3, 4, 3, 2, 1} element to remove = 2 Output: Up 2 min read How to Remove All Occurrences of an Element from Multiset in C++? In C++, a multiset is a container that stores a sorted collection of elements in sorted order, and we can also insert duplicate elements. In this article, we will learn how to remove all the occurrences of a specific element in C++. Example Input: myMultiset = {10, 10, 10, 20, 30, 40}; Target= 10 Ou 2 min read How to Remove Second Occurrence of an Element from a Vector in C++? In C++, vectors are the dynamic arrays that store the data in the contiguous memory location. It can also contain multiple copies of the same element. In this article, we will learn how to remove the second occurrence of an element from a vector in C++. Example: Input: myVector = {1, 2, 3, 4, 2, 5, 2 min read How to Replace All Occurences of an Element in a Set in C++? In C++, a set is an associative container that stores unique elements in a sorted order. In this article, we will learn how to replace all occurrences of a specific element in a set in C++. Example: Input: mySet = {1,2,3,2,4,5,2}; target = 2 replacement = 6 Output: After Replacement: 1 3 4 5 6 Repla 2 min read Like