How to Replace Second Occurrence of an Element from a Vector? Last Updated : 26 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 Occurrence of an Element in a VectorTo replace the second occurrence of a specific element in a std::vector in C++, we can use the std::find() function to find the first occurrence of the element, and then use std::find() again to find the second occurrence. Once we have found the second occurrence, we can replace it directly in the vector using the std::vector::erase() function. C++ Program to Replace the Second Occurrence of an Element in a VectorThe below example demonstrates the use of std::find() function to replace the second occurrence of a specific element in a vector in C++. C++ // C++ Program to illustrate how to replace the second // occurrence of a specific element in a vector. #include <algorithm> #include <iostream> #include <vector> using namespace std; void replaceSecondOccurrence(vector<int>& vec, int element, int replacement) { // Find the first occurrence auto it = find(vec.begin(), vec.end(), element); // If the element is found, find the second occurrence if (it != vec.end()) { it = find(it + 1, vec.end(), element); // If the second occurrence is found, replace it if (it != vec.end()) { *it = replacement; } } } int main() { // Vector declaration vector<int> vec = { 5, 2, 8, 5, 8, 8 }; // Element to replace and its replacement int element = 8; int replacement = 10; // Replacing the second occurrence of specific element replaceSecondOccurrence(vec, element, replacement); // Printing the vector after replacement cout << "Vector after replacing second occurrence of " << element << ": "; for (int i : vec) { cout << i << " "; } cout << endl; return 0; } OutputVector after replacing second occurrence of 8: 5 2 8 5 10 8 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Replace Second Occurrence of an Element from a Vector? sravankumar_171fa07058 Follow Improve Article Tags : C++ CPP Examples Practice Tags : CPP Similar Reads Find the First Occurrence of a Specific Element in a Vector in R In this article, we will discuss how to find the first occurrence of a specific element in a vector with its working example in the R Programming Language using R while loop. also explore various examples with detailed explanations to illustrate different concepts, principles, and scenarios across v 3 min read How to Create, Access, and Modify Vector Elements in R ? In this article, we are going how to create, modify, and access vectors in vector elements in the R Programming Language. Vector is a one-dimensional data structure that holds multiple data type elements. Creating a vectorIt can be done in these ways: Using c() function.Using: operator.Using the seq 5 min read How to Remove an Item from STL Vector with a Certain Value? In this article, we will learn the different methods to remove items with a certain value from vector in C++.The simplest method to remove all items with specific value from a vector is by using remove() method with vector erase(). Letâs take a look at an example:C++#include <bits/stdc++.h> us 3 min read How to create a new vector from a given vector in R In this article, we will discuss How to create a new vector from a given vector in R Programming Language. Create a new vector from a given vectorYou can use various functions and techniques depending on your needs to create a new vector from a given vector in R. Here are some common methods. 1. Sub 2 min read Find the Frequency of Each Element in a Vector in C++ The frequency of an element is number of times it occurred in the vector. In this article, we will learn different methods to find the frequency of each element in a vector in C++.The simplest method to find the frequency of each element in a vector is by iterating the vector and storing the count o 3 min read Like