How to Find the Second Occurrence of an Element in Vector in C++? Last Updated : 08 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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} Output: Second occurrence of element 10 is found at index 4Find the Second Occurrence of an Element in a Vector in C++ The C++ Standard Template Library(STL) provides a method std::find() which returns an iterator to the first occurrence of the specified element in the given range of values. We will use the find() method to find the second occurrence of our required element in the vector by simply running it two times. Syntax of find()find(first , last, value);first: iterator to the initial position of the range.last: iterator to position just after the final position of the range.value: value to be searched.C++ Program to Find the First Occurrence of an Element in Vector C++ // C++ program to demonstrate finding the index of the // second occurrence of a specific element in a vector #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { // Initialize a vector vector<int> vec = { 20, 30, 10, 50, 10, 80, 10 }; // declare the element you want to find int elementToFind = 10; // call the find function to search for the first // occurrence of the element and store the result in the // iterator auto it = find(vec.begin(), vec.end(), elementToFind); // If the first occurrence of the element is found if (it != vec.end()) { // call the find function again to search for the // second occurrence of the element it = find(it + 1, vec.end(), elementToFind); // If the second occurrence of the element is found, // print its index if (it != vec.end()) { cout << "Second occurrence of element " << elementToFind << " found at index: " << it - vec.begin() << endl; } else { cout << "Second occurrence of element not found" << endl; } } else { cout << "Element not found" << endl; } return 0; } OutputSecond occurrence of element 10 found at index: 4 Time Complexity: O(N) where N is the number of elements in the vectorAuxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find the Second Occurrence of an Element in Vector in C++? gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 How to Find the First Occurrence of an Element in a Vector? In C++, the vector is a dynamic array that is defined in the STL template library inside the <vector> header. In this article, we will learn how to find the first occurrence of a specific element in a vector in C++. For Example Input: vector<int>v = {5, 7, 1, 2, 3, 7, 1} Target = 1 Outpu 2 min read 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 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 Find All Occurrences of an Element in a Set in C++? Finding the all occurrences of a specific element in a set using the C++ STL is a very efficient process that is done with the help of std::set::distance() member function. In this article, we'll explore how to find the first element in a set using the C++ STL. For Example,Input:mySet = {1, 2, 4, 3, 2 min read Like