Calculate the Mode from recordset

# Thread: Calculate the Mode from recordset

1. droy Guest

## Calculate the Mode from recordset

Does anyone know a simple way to calculate the mode (most frequent number) from a set of numbers? For example, the mode of the following recordset is 2 (1,4,6,3,2,2,2,2) because it appears more times than any other number. I&#039d appreciate some help on approaching this using ASP. <BR><BR>Thanks.

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Calculate the Mode from recordset

I don&#039t think there is any clever way to do this. It&#039s just a matter of brute force coding.<BR><BR>I think what you&#039d do would be to create an array with element numbers matching the expected values. Then bump the count in each element as you find a value. Finally, find the maximum count in the array and record the element number as the mode.<BR>If the expected values form too wide a range to use an array (e.g., if they could range from -2000000000 to +2000000000), then you&#039d want to use a Scripting.Dictionary instead of an array, but otherwise the idea is the same.<BR><BR>What do you do if there is a tie?<BR><BR>

3. droy Guest

## RE: Calculate the Mode from recordset

That&#039s exactly what I&#039m doing to find the mode... I was hoping there might be a built-in function I was missing.<BR><BR>I also asked about creating an array based on the number of records being returned from my recordset. Do I just Dim the array with an arbitrary number and then ReDim it with the Recordset count?<BR><BR>Thanks.

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Calculate the Mode from recordset

&#062; Do I just Dim the array with an arbitrary number and then <BR>&#062; ReDim it with the Recordset count?<BR><BR>As I said, see the ASPFAQs:<BR>http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=7<BR>http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=63<BR><BR>You do *not* have to give an array any initial size.<BR><BR>&#060;%<BR>Dim ar( )<BR>ReDim ar( anyIntegerExpression )<BR>%&#062;<BR><BR>See also the VBScript documentation of the REDIM statement.<BR><BR>BUT...one thing you do *NOT* want to do is get the RecordSet.RecordCount value if you don&#039t have to!<BR><BR>Oh...and I just realized. Why do you care how many records there are? You don&#039t want to put each number into a unique cell in the array...you want to put COUNT each number by BUMPING the count in the matching cell. Do you *KNOW* what the RANGE of possible numbers is that you will try to get the MODE of??? If so, *that* is how many array elements you need. If not, then you&#039d probably be better off *not* using an array.<BR><BR>Again I ask: What do you do if there is a tie?<BR><BR><BR><BR><BR>

#### Posting Permissions

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