How to Find the Mode in a Sorted Array in C++? Last Updated : 16 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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} Output: Mode = 3 Input: myVector = {3, 5, 5, 7 , 8 , 8 , 8} Output: 8 Calculate the Mode of a Sorted Array in C++We can calculate the mode of the sorted array by counting the frequency of the adjacent elements as in a sorted array, all the equal numbers will be next to each other. Following is the complete approach to do that: ApproachInitialize an array with integer values.Initialize variables mode, curr_count, and max_count to track mode.Start iterating the array.If the current element is the same as the previous one, curr_count is incremented.If the current element is different, it checks if curr_count is greater than max_count.If it is, max_count and mode are updated.curr_count is then reset to 1 for the new element.Check if the last element forms the mode.Print the mode or a message if no mode is found.C++ Program Calculate the Mode of a Sorted Array in C++ C++ // C++ program to illustrate how to find the mode in a // sorted vector #include <iostream> using namespace std; int main() { // Initialize an array with integer values int myArray[] = { 1, 2, 3, 3, 5, 5, 5, 5, 6, 7 }; int size = sizeof(myArray) / sizeof(myArray[0]); // Initialize variables to track mode int mode = -1; int curr_count = 1; int max_count = 1; // Iterate through the array to find the mode for (int i = 1; i < size; ++i) { if (myArray[i] == myArray[i - 1]) { ++curr_count; } else { // Check if the current count is greater than // the maximum count found so far if (curr_count > max_count) { max_count = curr_count; mode = myArray[i - 1]; // Update the mode } curr_count = 1; // Reset current count for the // new element } } // Check if the last element forms the mode if (curr_count > max_count) { mode = myArray[size - 1]; } // Print the mode or a message if no mode is found if (mode != -1) { cout << "Mode is: " << mode << endl; } else { cout << "No mode found." << endl; } return 0; } OutputMode is: 5 Time Complexity: O(n), where n is the elements of the sorted array.Space Complexity: O(1) Comment More infoAdvertise with us Next Article How to Find the Mode in a Sorted Array in C++? B bhushanc2003 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads 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 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 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 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 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 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 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 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 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 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 Like