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 7525991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:42:35+00:00 2026-05-30T03:42:35+00:00

I need to write a function to find the mode of a array. I’m

  • 0

I need to write a function to find the mode of a array. I’m not good at coming up with algorithms however and I’m hoping someone else knows how to do this.

I know the size of the array and the values in each element, and I have the array sorted from least to greatest.

array would be passed to the mode function like

mode = findMode(arrayPointer, sizePointer);

UPDATE:

After reading the comments I’ve tried this

int findMode(int *arrPTR, const int *sizePTR)
{
    int most_found_element = arrPTR[0];
    int most_found_element_count = 0;
    int current_element = arrPTR[0];
    int current_element_count = 0;
    int count;
    for (count = 0; count < *sizePTR; count++)
    {
        if(count == arrPTR[count])
             current_element_count++;
        else if(current_element_count > most_found_element)
        {
            most_found_element = current_element;
            most_found_element_count = current_element_count;
        }
        current_element = count;
        current_element_count=1;
    }

    return most_found_element;
}

I’m still having problems grasping this algorithm though if anyone can sort me out.
I’ve never used vectors so don’t really understand the other examples.

  • 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-30T03:42:37+00:00Added an answer on May 30, 2026 at 3:42 am
    set most_found_element to the first element in the array
    set most_found_element_count to zero
    set current_element to the first element of the array
    set current_element_count to zero
    for each element e in the array
        if e is the same as the current_element
            increase current_element_count by one
        else
            if current_element_count is greater than most_found_element_count
                set most_found_element to the current_element
                set most_found_element_count to current_element_count
            set current_element to e
            set current_element_count to one
    if current_element_count is greater than most_found_element_count
        set most_found_element to the current_element
        set most_found_element_count to current_element_count
    print most_found_element and most_found_element_count
    

    I thought the names would explain it, but here we go:

    When we start, no element has been found the most times
      so the "high-score" count is zero.
    Also, the "current" value is the first, but we haven't looked at it yet 
      so we've seen it zero times so far
    Then we go through each element one by one
      if it's the same as "current" value, 
         then add this to the number of times we've seen the current value.
      if we've reached the next value, we've counted all of the "current" value.
         if there was more of the current value than the "high-score"
            then the "high-score" is now the current value
         and since we reached a new value
            the new current value is the value we just reached
    Now that we've seen all of the elements, we have to check the last one
      if there was more of the current value than the "high-score"
        then the "high-score" is now the current value
    Now the "high-score" holds the one that was in the array the most times!
    

    Also note: my original algorithm/code had a bug, we have to do an extra check of “current” after the loop ends, as it never finds “the one after the last”.

    • 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 function to find the first non-repeating character in a
I need to write function that will accept array of decimals and it will
I need to write a function that takes 4 bytes as input, performs a
I need to write a function that is passed an instance of a CharField.
I need to write a delegate function that can 'wrap' some while/try/catch code around
I need to write a little ruby function that does word wrapping. I have
I need to be able to write a function that shows repeated words from
I need to write a non-recursive version of the function sum-squares and Use a
Basically what I need to do is write a function that takes in a
I need to write, in JavaScript (I am using jQuery), a function that is

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.