Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
How to Find the Mode of Numbers in a Sorted Array in C?
Next article icon

How to Find the Mode of Numbers in a Sorted Array in C?

Last Updated : 18 Jun, 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 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;  } 

Output
Mode is: 5

Time Complexity: O(n), where n is the elements of the sorted array.
Space Complexity: O(1)




Next Article
How to Find the Mode of Numbers in a Sorted Array in C?

A

akshitsaxenaa09
Improve
Article Tags :
  • C Programs
  • C Language
  • c-array
  • C Examples

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences