# Thread: Modal value of a list

1. ## Modal value of a list

I need some mathmatical assistnace in finding the modal values of a list of numbers using VB.NET 2002 code. Say that you have a list of numbers:

10, 25, 30, 30, 20, 35, 20
20, 30, 25, 10, 25, 30, 30
35, 9, 7, 30, 10, 25, 20

now, to calculate the modal value, you must count the repetition of each number and the number that is repeated the most is the highest modal value, the next highest number that is repeated is the next highest modal value etc... so on and so on. In this list of numbers the number 10 is repeated 3 times and 25 is repeated 4 times and 30 is repeated 6 times and 20 is repeated 4 times and 35 is repeated 2 times and 20 is repeated 3 times and 9, 7 are only shown 1 time each.

From highest to lowest modal value the numbers would be:

30
25
20
10
20
35
9
7

Any one know of how this could be done and an example would be great on searching a list like this.

2. Are you going to implement this in a particular language? If so, then I think it's better to ask in the appropriate form.

A generic solution would be a lookup table, with a key and a value. The numbers you are encountering are stored as keys in the table. If the number isn't a key yet, add one to the table, and give it the value one. If the number is already a key, then increment the value with one. (In VB, create a user defined type, with the elements "key" and "value", and add them to a dynamic array. Use "Redim Preserve" to increment the array. In C++ you can use the stl::map)

When you're done, you need to sort the table by decrementing value. If you've done so, then the key of the first record will be the highest modal value. It can also happen that there are multiple values with the same modal value. You need to take that into account, depending on what you're going to do with the results.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•