How to Find Last Occurrence of an Element in a List in C++? Last Updated : 16 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In C++, std::list represents a doubly linked list and the last occurrence of an element in a list refers to the last time that element appears in the list when traversed from the beginning. In this article, we will learn how to find the last occurrence of a specific element in a list in C++. Example: Input: list<int> myList = {1, 2, 7, 4, 5, 7, 8} int key = 7; Output: The last occurrence of element 7 is found at index : 5Find the Last Occurrence of an Element in a List in C++To find the last occurrence of an element in a std::list, we can use the std::find() function with reverse iterators std::list::rbegin() and std::list::rend() to iterate through the list in reverse order. When we iterate in reverse order, the first occurrence is the last occurrence of that element. ApproachUse the std::find function along with reverse iterators to get the iterator pointing to the first occurrence of the target element from the last.If the element is found calculate its index using the std::distance function. Pass the base iterator (which points to one position past the end of the list in a forward direction) and the begin iterator to this function.Since list indexing is zero-based, subtract one from the result obtained from the std::distance function to get the correct index.Finally, print the index which denotes the last occurrence of the specific element in the list.C++ Program to Find the Last Occurrence of an Element in a ListThe below example demonstrates how to find the last occurrence of a specific element in a given list in C++. C++ // C++ program to demonstrate how to find the last // occurrence of a specific element in a given list #include <algorithm> #include <iostream> #include <list> using namespace std; int main() { // Initializing a list of integers list<int> myList = { 1, 2, 7, 4, 5, 7, 8 }; // Declare the element whose last occurrence is to be // found int element = 7; // Finding the last occurrence of the element auto it = find(myList.rbegin(), myList.rend(), element); // Printing the position of the last occurrence of the // element if (it != myList.rend()) { // Finding the index of the last occurrence of the // element int position = distance(myList.begin(), it.base()) - 1; cout << "The last occurrence of element " << element << " is found at index : " << position << endl; } else { cout << "Element not found in the list" << endl; } return 0; } OutputThe last occurrence of element 7 is found at index : 5 Time Complexity: O(n), here n is the number of elements in the list.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find Last Occurrence of an Element in a List in C++? R rohitpant4532 Follow Improve Article Tags : C++ Programs C++ STL cpp-list CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Find All Occurrences of an Element in a List in C++? In C++, std::list is a sequence container that allows non-contiguous memory allocation. As such, it is a doubly linked list that can be traversed in both directions. In this article, we will learn how to find all occurrences of a specific element in a list in C++. Example: Input: myList = {7, 5, 16, 2 min read How to Find First Occurrence of an Element in a List in C++? In C++, the list is a sequence container that stores data in non-contiguous memory allocation. It is defined in the STL (Standard Template Library) inside the <list> header. In this article, we will learn how to find the first occurrence of a specific element in a list in C++. Example: Input: 2 min read How to Find Last Occurrence of an Element in a Deque in C++? In C++, deques also known as double-ended queues are sequence containers that allow the users to insert and delete elements from both ends efficiently. In this article, we will learn how to find the last occurrence of a specific element in a deque in C++. Example Input: deque = {1, 2, 3, 4, 2, 5, 2, 2 min read How to Find All Occurrences of an Element in a Multiset in C++? In C++, a multiset is a container similar to a set but it allows multiple occurrences of its elements i.e. duplicate values. In this article, we will learn how to find all occurrences of a specific element in a multiset in C++. Example: Input: myMultiset = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};target = 3Ou 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 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 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 First Occurrence of an Element in a Set in C++? In C++, a set is an ordered container that stores its unique values in some given order. In this article, we will see how to find the first occurrence of a specific element in a set in C++ STL. For Example, Input: mySet = {1, 2, 3, 8, 9, 11} Target = 9 Output: Element found at Index: 4Find the First 2 min read How to Find First Occurrence of an Element in a Deque in C++? In C++, deques also known as double-ended queues are sequence containers with the feature of insertion and deletion on both ends. In this article, we will learn how to find the first occurrence of a specific element in a deque in C++. Example Input: myDeque ={2, 1, 5, 3, 4, 2, 5} Target=5 Output: Th 2 min read Like