![]() |
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 4 Find 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);
C++ Program to Find the First Occurrence of an Element in VectorC++
Output
Second occurrence of element 10 found at index: 4 Time Complexity: O(N) where N is the number of elements in the vector |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |