How to Find the Maximum Element in a List in C++? Last Updated : 16 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows us to store data in non-contiguous memory locations efficiently. In this article, we will learn how to find the maximum element in a list in C++. Example: Input: myList = {30, 20, 10, 50, 40}; Output: The maximum element in the list is: 50Finding the Maximum Element in a List in C++ To find the maximum element in a std::list, we can use the std::max_element() algorithm that returns an iterator pointing to the maximum value present in the list. Syntax to Find the Maximum Element in a List*max_element(begin, end);Here, begin and end are iterators denoting the range of elements in the list. C++ Program to Find the Maximum Element in a List The below program demonstrates how we can use the std::max_element function to find the maximum element in a list in C++. C++ // C++ program to demonstrates how we can use the // std::max_element function to find the maximum element in // a list #include <algorithm> #include <iostream> #include <list> using namespace std; int main() { // Initializing a list of integers list<int> myList = { 30, 20, 10, 50, 40 }; // Finding the maximum element int maxi = *max_element(myList.begin(), myList.end()); // Printing the maximum element of the list cout << "The maximum element in the list is : " << maxi << endl; return 0; } OutputThe maximum element in the list is : 50 Time Complexity: O(N), where N denotes the number of elements in the listAuxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find the Maximum Element in a List in C++? P pantharshx9d9 Follow Improve Article Tags : C++ Programs C++ STL cpp-list CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Find the Minimum Element in a List in C++? In C++, a list is a sequence container provided by the STL library that stores data in non-contiguous memory locations efficiently. In this article, we will learn how to find the minimum element in a list in C++. Example: Input: myList = {30, 10, 20, 50, 40};Output: The minimum element in the list i 2 min read How to Find the Maximum Element in a Deque in C++? in C++, double-ended queues, also known as deques, are sequence containers with the feature of insertion and deletion on both ends. They are similar to vectors but are more efficient for the insertion and deletion of elements from both ends. In this article, we will learn how to find the maximum ele 2 min read How to Find the Mode of All Elements in a List in C++? In C++, a list is a sequence container used to store data of similar type in non-contiguous memory locations. The mode of a list represents the element that occurs most frequently in the container. In this article, we will learn how to find the mode of a list in C++. Example: Input:myList = {1, 2, 3 2 min read How to Find Minimum Element in a Vector in C++? Given a vector of n elements, the task is to find the minimum element using C++.ExamplesInput: v = {2, 4, 1, 5, 3}Output: 1Explanation: 1 is the smallest element in the vectorInput: v = {12, 34, 5, 7}Output: 5Explanation: 5 is the smallest element in the vector.Following are the 6 different methods 4 min read How to Find the Maximum Key in a Map in C++? In C++, the Standard Template Library (STL) provides a map container to store elements in a mapped fashion so each element has a key value and a mapped value. In this article, we will see how to find the maximum key in a map in C++. Example: Input : myMap : {{1, 10}, {2, 20}, {4, 12}, {3, 44}}Output 2 min read How to Find the Second Smallest Element in an Array in C++? In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element 3 min read How to Find the Maximum Element of a Vector using STL in C++? Given a vector, find the maximum element of the vector using STL in C++. ExampleInput: v = {2, 4, 1, 5, 3}Output: 5Explanation: 5 is the largest element of vector.Input: v = {11, 23, 3, 5, 24}Output: 24Explanation: 24 is the largest element of the given range.STL provides the following different met 3 min read How to Find the Maximum Element of an Array using STL in C++? Given an array of n elements, the task is to find the maximum element using STL in C++.ExamplesInput: arr[] = {11, 13, 21, 45, 8}Output: 45Explanation: 45 is the largest element of the array.Input: arr[] = {1, 9, 2, 5, 7}Output: 9Explanation: 9 is the largest element of the array.STL provides the fo 3 min read How to Find Last Occurrence of an Element in a List in C++? 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 3 min read How to Find the Size of a List in C++? In C++, Standard Template Library (STL) we have a std::list container that is a doubly-linked list in which elements are stored in non-contiguous memory allocation. In this article, we will learn how to find the size of a list in C++ STL. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Size o 2 min read Like