How to Remove an Element from a Deque in C++? Last Updated : 27 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++ STL, a container called deque (known as a double-ended queue) allows us to insert and delete elements at both its beginning and its end. In this article, we will learn how to remove a specific element from a deque in C++ STL. Example: Input: myDeque= {4, 2, 3, 5, 2} Target = 4 Output: Deque After Removal of Target = 2 3 5 2Erase a Specific Element from a Deque in C++To remove a specific element from a deque in C++, we can use a combination of std::find() and the std::deque::erase() methods by finding the element first using the find() and then erasing it using the erase() function. If the element is at either of the end, the std::deque::erase() function is optimized for the fast removal as well. C++ Program to Remove a Specific Element from a Deque The below example demonstrates how we can remove a specific element from a deque in C++ STL. C++ // C++ program to demonstrate how we can remove a specific // element from a deque #include <algorithm> #include <deque> #include <iostream> using namespace std; int main() { // Initializing a deque deque<int> dq = { 4, 2, 3, 5, 2 }; // Element to remove int Target = 4; // Removing the element auto it = find(dq.begin(), dq.end(), Target); if (it != dq.end()) { dq.erase(it); } else { cout << "Element not found in the deque." << endl; } // Printing the result cout << "Deque after removal: "; for (int elem : dq) { cout << elem << " "; } cout << endl; return 0; } OutputDeque after removal: 2 3 5 2 Time Complexity: O(N), here N is the number of elements in the dequeAuxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Remove an Element from a Deque in C++? D denzirop9v Follow Improve Article Tags : C++ Programs C++ STL cpp-deque CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Dequeue an Element from a Queue in C++? In C++, queues are a type of container adaptors that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. In this article, we will learn how to dequeue an element from a queue in C++ STL. Example: Input: Create a queue an 2 min read How to Remove an Element from Front of Deque in C++? In C++, a deque (double-ended queue) is a data structure that allows efficient insertion and deletion at both ends. In this article, we will learn to remove an element from the beginning of a deque in C++. Example:Input: myDeque = {1, 2, 3, 4, 5}; Output: Deque After Removal: 2 3 4 5Removing the Fir 2 min read How to Remove an Element from the End of a Deque in C++? In C++, deque is also called a double-ended queue where we can insert new elements at both the front and back of the container. In this article, we will learn to remove an element from the end of a deque in C++. Example: Input: myDeque = {1, 2, 3, 4, 5} Output: Deque after removal: 1 2 3 4 Removing 2 min read How to Remove an Element from a Set in C++? In C++, sets are a type of associative container in which each element has to be unique. The values are stored in a specific sorted order i.e. either ascending or descending. In this article, we will see how to remove specific elements from a set in C++. Example Input: set = {100,120,12,56,78,9,32,4 2 min read How to Remove an Element from a List in C++? In C++, the STL provides a std::list container that represents a doubly linked list to store the sequential data in non-contiguous memory locations. In this article, we will learn how to remove an element from a list in C++. Example: Input: myList = {1, 2, 3, 4, 5, 6, 7, 8} Target = 5 Output: // rem 2 min read Like