Thread: Help with finding modal value

  1. #1
    Registered User
    Join Date
    Apr 2008

    Help with finding modal value

    I need to find an algorithm for finding the modal value of an array of numbers. I have the array organized in an ascending order. I suspected that the algorithm would be something like what you would use for a bubble sort, but I can't seem to make it work. Hopefully somebody can clue me in. Thanks.

    The array is a total of 10 numbers.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    The thing to note is that if your array is sorted, all the like numbers will have to be next to each other. So scan through the array looking for clumps of numbers -- the largest clump(s) is(are) the mode(s).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Yeah, I tried doing that by using a series of for loops with an if statement attached, but I can't seem to get it right.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005

    Perhaps the best way to find the mode would be to sort the numbers first (perhaps with a bubble sort like you mentioned), and then step through it to find the number that occurs the most. This latter step is pretty easy to do. You need a loop to iterate through the elements in the array, a maximum detected count, and perhaps a current count.

    I suppose you could do this without sorting the array, but it would be rather difficult.

    Personally, I might use a modified selection sort for this. It would let you sort the numbers and count how many occurrences of each number there are at the same time. The "sorted" array would just have to have a count associated with each number. For every element in the array, you would find the place in the sorted array that you would insert the new element. If the element already exists, you would increment the counter for that number instead of adding a new number to the list . . . .

    Just some thoughts I had at first glance.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding primes
    By starripper in forum C++ Programming
    Replies: 19
    Last Post: 01-14-2006, 04:17 PM
  2. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM
  3. Modal Dialogs?
    By jinx in forum Windows Programming
    Replies: 1
    Last Post: 11-29-2001, 02:49 PM
  4. Getting Modal text in and SDI app.
    By jinx in forum Windows Programming
    Replies: 3
    Last Post: 11-23-2001, 12:01 PM
  5. modal average
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2001, 01:18 PM