How to Find the Mode in a 2D Array in C++? Last Updated : 08 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 can use the unordered_map to keep the count of the frequency of the elements. Traverse a given 2D array and check if the current element exists in the unordered_map.If the element does not exist, add the element as the key and count (which is one yet) as its value.If the element exists, then increment the frequency.Finally, traverse through a map and find the number that has the highest frequency which is called our mode.Example The below example demonstrates the use of map to find multiple modes if exist in a 2D array. C++ // C++ program to find the mode of a 2D array #include <iostream> #include <unordered_map> using namespace std; // Function to find the mode of a 2D array int findMode(int arr[][4], int rows) { unordered_map<int, int> freq; // Count the frequency of each number in the array for (int i = 0; i < rows; ++i) { for (int j = 0; j < 4; ++j) { freq[arr[i][j]]++; } } // Find the number with the highest frequency (mode) int mode = arr[0][0]; int maxCount = 0; for (auto it = freq.begin(); it != freq.end(); ++it) { if (it->second > maxCount) { maxCount = it->second; mode = it->first; } } return mode; } int main() { // Example 2D array int arr[3][4] = { { 1, 2, 2, 3 }, { 3, 4, 5, 5 }, { 5, 6, 7, 8 } }; // Find and display the mode of the array cout << "Mode: " << findMode(arr, 3) << endl; return 0; } OutputMode: 5 Time Complexity: O(N * M)Auxiliary Space: O(N * M) We can also create a max pair that keeps the element and count of the currently maximum encountered value. But it may fail if there are multiple modes. Comment More infoAdvertise with us Next Article How to Find the Median of 2D Array in C++? S susobhanakhuli19 Follow Improve Article Tags : C++ Programs C++ cpp-array cpp-unordered_map CPP Examples +1 More Practice Tags : CPP Similar Reads 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 Median of 2D Array in C++? The median can be defined as the middle element of a sorted array in the case of an odd number of elements in an array and the average of the middle two elements when the number of elements in an array is even. In this article, we will see how to calculate the median of a 2D array in C++. Example: I 3 min read How to Find the Mode of Numbers in an Array in C++? 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 elemen 2 min read How to Find the Range of Values in a 2D Array in C++? In C++, 2D arrays are also known as a matrix, and the range of numbers in a 2D array means the maximum and the minimum value in which the numbers lie in the given 2D array. In this article, we will learn how to find the range of numbers in a 2D array in C++. For Example, Input: my2DArray= {{80, 90, 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 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 Like