Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1096311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:15:55+00:00 2026-05-17T00:15:55+00:00

I need to write a program to find the mode. Or the most occurrence

  • 0

I need to write a program to find the mode. Or the most occurrence of an integer or integers.
So,

1,2,3,4,1,10,4,23,12,4,1 would have mode of 1 and 4.

I’m not really sure what kind of algorithm i should use. I’m having a hard time trying to think of something that would work.

I was thinking of a frequency table of some sort maybe where i could go through array and then go through and create a linked list maybe. If the linked doesn’t contain that value add it to the linked, if it does then add 1 to the value.

So if i had the same thing from above. loop through
1,2,3,4,1,10,4,23,12,4,1

Then list is empty so add node with number = 1 and value = 1.
2 doesnt exist so add node with number = 2 and value = 1 and so on.
Get to the 1 and 1 already exists so value = 2 now.

I would have to loop through the array and then loop through linked list everytime to find that value.

Once i am done then go through the linked list and create a new linked list that will hold the modes. So i set the head to the first element which is 1. Then i go through the linked list that contains the occurences and compare the values. If the occurences of the current node is > the current highest then i set the head to this node. If its = to the highest then i add the node to the mode linked list.

Once i am done i loop through the mode list and print the values.

Not sure if this would work. Does anyone see anything wrong with this? Is there an easier way to do this? I was thinking a hash table too, but not really sure how to do that in C.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:15:55+00:00Added an answer on May 17, 2026 at 12:15 am

    The algorithm you have is fine for a homework assignment. There are all sorts of things you could do to optimise the code, such as:

    • use a binary tree for efficiency,
    • use an array of counts where the index is the number (assuming the number range is limited).

    But I think you’ll find they’re not necessary in this case. For homework, the intent is just to show that you understand how to program, not that you know all sorts of tricks for wringing out the last ounce of performance. Your educator will be looking far more for readable, structured, code than tricky optimisations.

    I’ll describe below what I’d do. You’re obviously free to use my advice as much or as little as you wish, depending on how much satisfaction you want to gain at doing it yourself. I’ll provide pseudo-code only, which is my standard practice for homework questions.

    I would start with a structure holding a number, a count and next pointer (for your linked list) and the global pointer to the first one:

    typedef struct sElement {
        int number;
        int count;
        struct sElement *next;
    } tElement;
    tElement first = NULL;
    

    Then create some functions for creating and using the list:

    tElement *incrementElement (int number);
    tElement *getMaxCountElement (void);
    tElement *getNextMatching (tElement *ptr, int count);
    

    Those functions will, respectively:

    • Increment the count for an element (or create it and set count to 1).
    • Scan all the elements returning the maximum count.
    • Get the next element pointer matching the count, starting at a given point, or NULL if no more.

    The pseudo-code for each:

    def incrementElement (number):
        # Find matching number in list or NULL.
    
        set ptr to first
        while ptr is not NULL:
            if ptr->number is equal to number:
                return ptr
            set ptr to ptr->next
    
        # If not found, add one at start with zero count.
    
        if ptr is NULL:
            set ptr to newly allocated element
            set ptr->number to number
            set ptr->count to 0
            set ptr->next to first
            set first to ptr            
    
        # Increment count.
    
        set ptr->count to ptr->count + 1
    

     

    def getMaxCountElement (number):
        # List empty, no mode.
    
        if first is NULL:
            return NULL
    
        # Assume first element is mode to start with.
    
        set retptr to first
    
        # Process all other elements.
    
        set ptr to first->next
        while ptr is not NULL:
            # Save new mode if you find one.
    
            if ptr->count is greater than retptr->count:
                set retptr to ptr
            set ptr to ptr->next
    
        # Return actual mode element pointer.
    
        return retptr
    

     

    def getNextMatching (ptr, number):
        # Process all elements.
    
        while ptr is not NULL:
            # If match on count, return it.
    
            if ptr->number is equal to number:
                return ptr
            set ptr to ptr->next
    
        # Went through whole list with no match, return NULL.
    
        return NULL
    

    Then your main program becomes:

    # Process all the numbers, adding to (or incrementing in) list .
    
    for each n in numbers to process:
        incrementElement (n)
    
    # Get the mode quantity, only look for modes if list was non-empty.
    
    maxElem = getMaxCountElement ()
    if maxElem is not NULL:
        # Find the first one, whil exists, print and find the next one.
    
        ptr = getNextMatching (first, maxElem->count)
        while ptr is not NULL:
            print ptr->number
            ptr = getNextMatching (ptr->next, maxElem->count)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a program that uses brute-force method to find out how
I need to write a C program to find the Perfect Number.. main() {
I need to write a program that prints 0.(03) for input 1 and 33.
I need to write a program that takes input line at a time and
i need to write a program in c# to collect information about processes running
I need to write a program that uses matrix multiplication to rotate an image
I need to write this program using a loop https://i.stack.imgur.com/gko6T.jpg this is what i
I'm going to need to write a program that takes a list of persons
I need to write one java program which monitors a folder containing excel sheets
I need to write a little program in C that parses a string. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.