![]() |
VOOZH | about |
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 learn how to find the mode of all elements in a sorted array of integers in C.
Example:
Input:
myArray = {1, 2, 3, 3, 5, 5, 5, 5, 6, 7}
Output:
Mode is 5
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.
- Initialize 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.
Mode is: 5
Time Complexity: O(n), where n is the elements of the sorted array.
Space Complexity: O(1)