How to Remove Second Occurrence of an Element from a Vector in C++? Last Updated : 26 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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}; Key = 2 Output: Elements before removing target is: {1, 2, 3, 4, 2, 5, 2}; Elements after removing target is: {1, 2, 3, 4, 5, 2};Remove Second Occurrence of an Element from a Vector in C++To remove the second occurrence of an element from a vector in C++, we can use the combination of std::find() and std::vector::erase() functions. We can use the std::find() function to find the second occurrence and then use the std::vector::erase() function to remove the second occurence. C++ Program to Remove Second Occurrence of an Element from a Vector C++ #include <algorithm> #include <iostream> #include <vector> using namespace std; // Function to remove the second occurrence of an element // from a vector void removeSecondOccurrence(vector<int>& vec, int element) { // Find the first occurrence of the element auto it = find(vec.begin(), vec.end(), element); // If the element is found, find the second occurrence if (it != vec.end()) { // Advance the iterator to the next element to find // the second occurrence it = find(it + 1, vec.end(), element); // If the second occurrence is found, erase it if (it != vec.end()) { vec.erase(it); } } } int main() { // Create a vector with some elements vector<int> vec = { 1, 2, 3, 2, 4, 2, 5 }; // Print the original vector cout << "Original vector: "; for (int i : vec) { cout << i << " "; } cout << endl; // Call the removeSecondOccurrence function to remove // the second occurrence of 2 removeSecondOccurrence(vec, 2); // Print the modified vector after removing the second // occurrence of 2 cout << "After removing second occurrence of 2: "; for (int i : vec) { cout << i << " "; } cout << endl; return 0; } OutputOriginal vector: 1 2 3 2 4 2 5 After removing second occurrence of 2: 1 2 3 4 2 5 Time Complexity: O(N)Space Complexity: O(1) Comment More infoAdvertise with us Next Article How to Remove All Occurrences of an Element from a Set in C++? T the_star_emperor Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Replace Second Occurrence of an Element from a Vector? In C++, a vector is a dynamic array that can grow and shrink in size as needed. In this article, we will learn how to replace the second occurrence of a specific element in a vector. Example: Input: myVector = { 5,2,8,5,8,8} Element: 8 Replacement: 10 Output: myVector = { 5,2,8,5,10,8}Replace Second 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 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 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 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 Like