How to Find the Mode of Numbers in a Sorted Array in C? (original) (raw)
Last Updated : 18 Jun, 2024
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
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.
Approach
- 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.
C Program to Find the Mode of the Sorted Array
C `
#include <stdio.h>
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) {
printf("Mode is: %d" , mode) ;
}
else {
printf( "No mode found.") ;
}
return 0;
}
`
**Time Complexity: O(n), where n is the elements of the sorted array.
**Space Complexity: O(1)
Similar Reads
- How to Find the Mode of Numbers in an Array in C? In C, the mode of array numbers is the element that appears most frequently in the array. To find the mode, we can count the occurrences of each element and identify the one with the highest count. In this article, we will find the mode of numbers in C. Example:Input: myArray = { 1, 2, 3, 4, 5, 2, 3 5 min read
- How to Find the Range of Numbers in an Array in C? The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra 2 min read
- How to Find Median of Numbers in an Array in C? For an odd number of elements in an array, the median is the middle element, and for an even number, it’s the average of the two middle elements. In this article, we will learn how to find median of numbers in an array in C.The median of an array can be determined is by first sorting the array and t 3 min read
- How to Find the Size of an Array in C? The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.The simplest method to find the size of an array in C is by using sizeof operator. First determine t 2 min read
- How to Find Maximum Value in an Array in C? In C, arrays are data structures that allow the user to store a collection of data of the same type. In this article, we will learn how we can find the maximum value in an array in C. Example Input: arr = {5,3,1,2,4} Output: The maximum value of the array is: 5Finding Maximum Value in an Array in CW 2 min read
- How to Sort an Array of Structs Based on a Member in C? In C, we may sometimes need to sort the array of structs based on the values of certain members. In this article, we will learn how to sort a given array of structures based on a specific member in C. For Example, Input: myArrayofStructs[] = {{"Person1", 21, 160.5}, {"Person2", 20, 175.0}, {"Person3 2 min read
- C Program to Sort an Array in Ascending Order Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element.The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example:C 3 min read
- C Program to Sort the Elements of an Array in Descending Order Sort an array in descending order means arranging the elements in such a way that the largest element at first place, second largest at second place and so on. In this article, we will learn different ways to sort an array in descending order in C.The simplest method to sort the array in descending 3 min read
- C Program to Find the Maximum and Minimum Element in the Array In this article, we will discuss different ways to find the maximum and minimum elements of the array in C.The simplest method to find the maximum and minimum element of the array is iterates through the array and compare each element with the assumed minimum and maximum and update them if the curre 3 min read
- How to Sort an Array of Structs with qsort in C? Sorting C arrays of structs becomes important for many kinds of applications and one common library function that may be used for this is qsort. In this article, we will learn how to use qsort() with an array of structs in C. For Example, Input: struct Person people[] = {{"Person1", 21}, {"Person2", 2 min read