How to Remove All Occurrences of an Element from List in C++? Last Updated : 18 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 Element from a List in C++To remove all occurrences of an element from a list in C++, we can use the remove() member function provided by the list class in the STL. This function removes all elements with a certain value from the list. C++ Program to Remove an Element from a ListThe following program illustrates how we can use the list::remove() function to remove an element from a list: C++ // C++ Program to illustrate how to remove all // occurrences of an element from a list #include <iostream> #include <list> using namespace std; int main() { // Initializing a list list<int> myList = { 1, 2, 3, 3, 4, 5 }; // element to remove int target = 3; // using remove() to delete all occurrences of the // element myList.remove(target); // printing the list after removal cout << "The list after removing all occurrences of " "the element " << target << " is: "; for (int val : myList) cout << val << " "; return 0; } OutputThe list after removing all occurrences of the element 3 is: 1 2 4 5 Time complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Remove All Occurrences of an Element from List in C++? S sravankumar_171fa07058 Follow Improve Article Tags : C++ Programs C++ STL cpp-list CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 All Occurrences of an Element from 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 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 Elemen 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 Last Occurrence of an Element from a Vector in C++? In C++, vectors are the same as dynamic arrays with the ability to resize automatically when an element is inserted or deleted, with their storage being handled automatically by the container. In this article, we will learn how to remove the last occurrence of a specific element in a vector. Input: 2 min read How to Replace All Occurrences of an Element in a List in C++? In C++, a std::list that represents a doubly linked list is a sequence containers that allow non-contiguous memory allocation. In this article, we will learn how to replace all the occurrences of a specific element in a list using C++ STL. Example: Input: myList = {10, 20, 30, 10, 30, 30, 50}; oldEl 2 min read Like