How to Find the Mode of Vector in C++? Last Updated : 26 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a vector is a dynamic array that resizes itself when needed. It also allows random access to its elements. In this article, we will learn how to find the mode of a vector in C++. The mode of a vector is the element that highest number of occurrences in the vector. Example: Input: myVector = { 1, 2, 3, 4, 5, 2, 3, 2, 2, 4, 2 } Output: Mode : 2Finding Mode of Vector in C++We can find the mode of a vector in C++ using a std::map container to count the frequency of each element in the vector, and then find the element with the maximum frequency. C++ Program for Finding Mode of Vector in C++ C++ // C++ Program to find mode of all Elements in a vector #include <iostream> #include <unordered_map> #include <vector> using namespace std; // Function to find the mode of a vector int findMode(const vector<int>& nums) { // Create an unordered map to store frequency of each // element unordered_map<int, int> frequency; // Iterate through the vector and update frequency of // each element for (int num : nums) { frequency[num]++; } // Initialize variables to keep track of mode and its // frequency int mode = 0; int maxFrequency = 0; // Iterate through the unordered map and find the // element with maximum frequency for (const auto& pair : frequency) { if (pair.second > maxFrequency) { maxFrequency = pair.second; mode = pair.first; } } // Return the mode return mode; } // Driver Code int main() { // Create a vector of numbers vector<int> numbers = { 1, 2, 3, 4, 5, 2, 3, 2, 2, 4, 2 }; // Find the mode of the vector int mode = findMode(numbers); // Print the mode cout << "Mode: " << mode << endl; return 0; } OutputMode: 2 Time complexity: O(N)Space complexity: O(N) Comment More infoAdvertise with us Next Article How to Find the Mode of Vector in C++? A anuragvbj79 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector cpp-map CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Find the Median of Sorted Vector in C++? In C++, vectors are dynamic arrays and the median is a middle value when data is sorted in ascending order. In this article, we will learn how to find the median of all elements in a sorted vector in C++. Example Input: myVector = {10, 20, 30, 40, 50}; Output: Median is 30Finding Median of Sorted Ve 2 min read How to Find the Size of a Vector in C++? The size of a vector means the number of elements currently stored in the vector container. In this article, we will learn how to find the size of vector in C++.The easiest way to find the size of vector is by using vector size() function. Letâs take a look at a simple example:C++#include <bits/s 2 min read How to Find the Median of Vector Elements in C++? Median of a dataset is defined as the middle element when N(size of vector) is odd and the average of the middle two elements when N is even in the sorted dataset. In this article, we will learn how to find the median of all the elements of a vector in C++. For Example Input:myVector = {3, 1, 6, 2, 2 min read How to Find the Capacity of a Vector in C++? In C++, the capacity of a vector means the amount of space allocated for the vector, so the vector can hold more elements than its size without any reallocation of space. In this article, we will learn how to find the capacity of a vector in C++. Example: Input: myVector = {10,20,30}; Output: Capaci 2 min read How to Find the Size of a Vector in Bytes in C++? In C++, Vectors are dynamic containers that can change their size during the insertion or deletion of elements. In this article, we will explore how we can find the size of the vector in bytes in C++. Example: Input:myVector = {10,20,30,40,50}Output:Size of the vector in bytes is : 20 bytesFind the 2 min read Like