How to Remove Last Occurrence of an Element from a Vector in C++? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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: myVector = { 5,2,8,5,8,8} Element: 8 Output: myVector = { 5,2,8,5,8}Remove Last Occurrence of an Element from Vector in C++To remove the last occurrence of an element in a std::vector in C++, we can use the std::find() function in combination with std::vector::erase(). The std::find() function returns an iterator pointing to the first occurrence of the element if it is found. If the element is not found, it returns an iterator pointing to the end of the vector. The std::vector::erase() function removes the element pointed to by the iterator. ApproachWe will pass the reverse_iterators using vector::rbegin() and vector::rend() to the find() function.Then, if the element is found, it will be the last occurrence of that element due to being traversed from the end to start.We will then pass this iterator to the std::vector::erase() function which will remove it from the vector. C++ Program to Remove Last Occurrence of an Element from Vector in C++ C++ // C++ Program to remove all occurrences of a specific // element from a vector #include <algorithm> #include <iostream> #include <vector> using namespace std; void removeLastOccurrence(vector<int>& vec, int element) { auto it = find(vec.rbegin(), vec.rend(), element); if (it != vec.rend()) vec.erase((it + 1).base()); } int main() { // Vector declaration vector<int> vec = { 5, 2, 8, 5, 8, 8 }; // Element to remove int element = 8; // Removing the last occurrence of specific element removeLastOccurrence(vec, element); // Printing the vector after removal cout << "Vector after removing last occurrence of " << element << ": "; for (int i : vec) { cout << i << " "; } cout << endl; return 0; } OutputVector after removing last occurrence of 8: 5 2 8 5 8 Time Complexity: O(n),Space Complexity: O(1) Comment More infoAdvertise with us Next Article How to Remove Last Occurrence of an Element from a Vector in C++? lunatic1 Follow Improve Article Tags : C++ Programs C++ CPP Examples Practice Tags : CPP Similar Reads 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 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 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 Find Last Occurrence of an Element in Vector? In C++, the last occurrence of an element in a vector means the last time that element appeared in the vector if traversed from the start. In this article, we will learn how to find the last occurrence of a specific element in a vector in C++. Example Input:vector<int> vec= {2, 5, 9, 8, 5, 4, 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 Element from Vector in C++? Given a vector of n elements, the task is to remove the last element from the vector in C++.The most efficient method to remove the last element from vector is by using vector pop_back() method. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { 2 min read How to Find the Second Occurrence of an Element in Vector in C++? In C++, vector containers store the data in a contiguous memory location like arrays and also can resize themselves to store more elements. In this article, we will learn how to find the second occurrence of a specific element in a vector in C++. Example Input: myVector = {20, 30, 10, 50, 10, 80, 10 2 min read How to Find the Last Occurrence of an Element in a Set in C++? In C++, a set is a container that stores unique elements in a sorted order and elements are accessed and traversed using iterators. In this article, we will learn how to find the last occurrence of a specific element in a set in C++. Example Input:set<int> s = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Key 2 min read Like