How to Find the Mode of Numbers in an Array in C++? Last Updated : 14 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Mode of any dataset is the item that occurs most frequently in it. In this article, we will find the mode of numbers in an unsorted array in C++. For Example,Input: myArray = { 1, 2, 3, 4, 5, 2, 3, 2, 2, 4, 2 } Output: Mode : 2Finding Mode of Array Elements in C++To find the mode of the array elements, we need to count the frequency of all elements in the array. To do that, we can use the std::unordered_map container where the key will represent the array element and its value will represent its frequency. ApproachCreate an unordered_map of <int, int>.Start traversing the array.Now, if the array element is present in the unordered_map, increment its value.If the array element is not present, add the array element as key with value 1.Finally, use std::max_element algorithm to find the maximum frequency and hence mode of the numbers.C++ Program to Find Mode of Array Elements C++ // C++ program to find the mode of an array #include <algorithm> #include <iostream> #include <unordered_map> using namespace std; // Function to find the mode of an array int findMode(int arr[], int n) { // Create a frequency map to count occurrences of each // element unordered_map<int, int> freqMap; for (int i = 0; i < n; i++) { freqMap[arr[i]]++; } // Find the element with the maximum frequency auto maxElement = max_element(freqMap.begin(), freqMap.end(), [](const auto& a, const auto& b) { return a.second < b.second; }); // Return the mode (element with maximum frequency) return maxElement->first; } int main() { // Test array int arr[] = { 2, 2, 3, 3, 3, 4 }; int n = sizeof(arr) / sizeof(arr[0]); // Find and print the mode of the array cout << "Mode of the array is " << findMode(arr, n) << endl; return 0; } OutputMode of the array is 3 Time Complexity: O(n)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article How to Find the Mode of Numbers in an Array in C++? anuragvbj79 Follow Improve Article Tags : C++ Programs C++ STL cpp-array cpp-unordered_map CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Find the Range of Numbers in an Array in C++? The range of numbers in an array means the difference between the maximum value and the minimum value in the given array. In this article, we will learn how to find the range of numbers in an array in C++. For Example, Input: myArray = {5,10,15,30,25}; Output: Range: 25Range Within an Array in C++To 2 min read How to Find the Mode in a Sorted Array in C++? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will discuss how to calculate the mode of the numbers in a sorted array in C++. Example: Input: myVector = {1, 2, 2, 3, 3, 3, 4, 4, 5} Outp 3 min read How to Find the Mode in a 2D Array in C++? A mode is a number that occurs most frequently in comparison to other numbers in a given dataset. In this article, we will find the mode in a 2D array of integers in C++. Input:myArray = { {1, 2, 2, 3}, {3, 4, 5, 5}, {5, 6, 7, 8} }Output: 5Mode in 2D ArrayTo find a mode of numbers in a 2D array, we 2 min read How to Find the Smallest Number in an Array in C++? In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma 2 min read How to Find Largest Number in an Array in C++? In C++, arrays are used to store the collection of similar elements to be stored in adjacent memory locations. They can store data of any type such as int, char, float, etc. In this article, we will learn how to find the largest number in an array in C++. For Example,Input: myVector = {1, 3, 10, 7, 2 min read How to Find the Length of an Array in C++? In C++, the length of an array is defined as the total number of elements present in the array. In this article, we will learn how to find the length of an array in C++.The simplest way to find the length of an array is by using the sizeof operator. First calculate the size of the array in bytes and 2 min read How to Find the Median of Array in C++? In C++, the array is a collection of elements of the same type, In this article, we will learn how to find the median of the array in C++. The median of the array will be the middle element if the number of elements is odd or the average of two middle elements if the number of elements is even in th 2 min read How to Find the Third Smallest Number in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the third smallest element in an array in C++. If there is no third smallest elemen 2 min read How to Find the Median of a Sorted Array in C++? In C++, the median of a sorted array is the middle element if the array size is odd, or the average of the two middle elements if the array size is even. In this article, we will learn how to find the median of a sorted array in C++. Example Input: myArray: {1, 2, 3, 4, 5} Output: Median of the Arra 2 min read How to Find the Sum of Elements in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} O 2 min read Like