# How to find the Mode of an array of numbers • ### Question

• Hi, how do i find the Mode of an array of numbers

this is what i have so far, sacount1 is the max index

For j = 1 To SAcount1
If NFAI(j) = NFAI(j - 1) Then
x1 = x1 + 1
End If
Next
lblMode.Text = x1

Thursday, May 6, 2010 10:26 PM

• Lemmy,

I might be the only one here who doesn't, but I have no idea what you're referring to "mode of an array". You might want to explain more about what you're looking for.

As for the code there - you are aware that arrays are zero-based, right?

• Marked as answer by Thursday, May 6, 2010 10:50 PM
Thursday, May 6, 2010 10:45 PM

### All replies

• Lemmy,

I might be the only one here who doesn't, but I have no idea what you're referring to "mode of an array". You might want to explain more about what you're looking for.

As for the code there - you are aware that arrays are zero-based, right?

• Marked as answer by Thursday, May 6, 2010 10:50 PM
Thursday, May 6, 2010 10:45 PM
• Hi Lemmy Winks ,

Why not click the green triangle instead and mark as helpful ?

Sorry Frank.

The MODE of any collection is the item that occurs most often

so from this set; {3,3,3,3,4,2,2,2,1}

3 is the MODE

4 is the MEDIAN or middle number.

I have not calculated the AVERAGE.

Regards,

John

Thursday, May 6, 2010 11:36 PM
• Hi Lemmy Winks ,

Why not click the green triangle instead and mark as helpful ?

Sorry Frank.

The MODE of any collection is the item that occurs most often

so from this set; {3,3,3,3,4,2,2,2,1}

3 is the MODE

4 is the MEDIAN or middle number.

I have not calculated the AVERAGE.

Regards,

John

No, no offense John, I didn't think about statistics. But if in fact that's what he was asking - then how in creation did I help?

I dunno. :-o

Thursday, May 6, 2010 11:47 PM
• @ Lemmy ->

Just "for the record here" ... please don't feel embarrassed to ask whatever comes to mind (well, "what's the meaning of life" might take a while). Trust me - I consider myself to be the veritable village idiot here, so you're in good company. LOL :)

Thursday, May 6, 2010 11:48 PM
• There may be a better way to do this, but I have the following:

```    Dim numbers() As Integer = {3, 3, 3, 3, 4, 2, 2, 2, 1}
Dim counts = numbers.GroupBy(Function(n) n).Select(Function(g) New With {.Number = g.Key, .Quantity = g.Count})

For Each count In counts
Debug.WriteLine(count.Number & ": " & count.Quantity)
Next

Dim mode = numbers.GroupBy(Function(n) n).Select(Function(g) New With {.Number = g.Key, .Quantity = g.Count}).OrderByDescending(Function(o) o.Quantity).FirstOrDefault
Debug.WriteLine("Mode is: " & mode.Number)```

The first set of code defines a list of the numbers and their counts.

The last two lines of code calculate and display the mode.

Hope this helps.

www.insteptech.com ; msmvps.com/blogs/deborahk
Friday, May 7, 2010 1:34 AM
• Hi ALL,

In support of the excellent post by DeborahK above, just to add.

OPTION INFER ON

is required.

Here is the output shown with MessageBoxes instead using one button on a FORM.

The following code was tried in both 2008 and 2010 versions of VB.Net.  :-D

```Option Strict On
Option Infer On

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim numbers() As Integer = {3, 3, 3, 3, 4, 2, 2, 2, 1}
Dim counts = numbers.GroupBy(Function(n) n).Select(Function(g) New With {.Number = g.Key, .Quantity = g.Count})

Dim sb As New System.Text.StringBuilder
For Each count In counts
sb.Append("Count of " & count.Number & "'s = " & count.Quantity & System.Environment.NewLine)
Next
MessageBox.Show(sb.ToString)

Dim mode = numbers.GroupBy(Function(n) n).Select(Function(g) New With {.Number = g.Key, .Quantity = g.Count}).OrderByDescending(Function(o) o.Quantity).FirstOrDefault
MessageBox.Show("Mode is: " & mode.Number)

End Sub
End Class```

Regards,

John

P.S. To DeborahK , do you know of any function that would get the middle or MEDIAN value if an array has an odd number of items at all please? Sorry, to ask but I just love seeing these Functions from you.

;-)

I tried it with DOUBLE values too. :-D

>>

```Option Strict On
Option Infer On

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim numbers() As Double = {3.2, 3.2, 3.2, 3.2, 4.5, 2.7, 2.7, 2.7, 1.6}
Dim counts = numbers.GroupBy(Function(n) n).Select(Function(g) New With {.Number = g.Key, .Quantity = g.Count})

Dim sb As New System.Text.StringBuilder
For Each count In counts
sb.Append("Count of " & count.Number & "'s = " & count.Quantity & System.Environment.NewLine)
Next
MessageBox.Show(sb.ToString)

Dim mode = numbers.GroupBy(Function(n) n).Select(Function(g) New With {.Number = g.Key, .Quantity = g.Count}).OrderByDescending(Function(o) o.Quantity).FirstOrDefault
MessageBox.Show("Mode is: " & mode.Number)

End Sub
End Class```

Friday, May 7, 2010 3:55 AM
• John -

I am working on the median now ... but I think your original most about this was not right. The median of this {3,3,3,3,4,2,2,2,1} is not 4, right? It would be 3?

www.insteptech.com ; msmvps.com/blogs/deborahk
Friday, May 7, 2010 6:13 AM
• John -

I am working on the median now ... but I think your original most about this was not right. The median of this {3,3,3,3,4,2,2,2,1} is not 4, right? It would be 3?

www.insteptech.com ; msmvps.com/blogs/deborahk

Hi again DeborahK ,

You are of course right after reading.>>

it is the middle number when the numbers are sorted in increasing value when the number of items is odd.

I had forgot to "arrange" them in order first, I'm bad, doh!!

Otherwise the MEDIAN is the average of the two numbers in the middle of a sequence if the numbers of items is even when the numbers are sorted in increasing order first.

I had forgotton all that you know, something to do with middle age I think!! LOL!!

Regards,

John

Friday, May 7, 2010 6:31 AM
• I posted the mean, median, and mode here:

http://msmvps.com/blogs/deborahk/archive/2010/05/07/linq-mean-median-and-mode.aspx

Hope this helps.

www.insteptech.com ; msmvps.com/blogs/deborahk
Friday, May 7, 2010 7:24 AM
• I posted the mean, median, and mode here:

http://msmvps.com/blogs/deborahk/archive/2010/05/07/linq-mean-median-and-mode.aspx

Hope this helps.

www.insteptech.com ; msmvps.com/blogs/deborahk

Hi again DeborahK ,

Many thanks once again. :-D

I reckon you enjoy creating these LINQ and LAMBDA expressions?  ;-)

I reckon you should create a DeborahK_methods_library.dll

Maybe make it a download on your blog site?  I'd even be willing to pay for it via PayPal.

Have you considered or have you wrote any add-ins for Visual Studio?

Regards,

John

Friday, May 7, 2010 10:36 PM
• Maybe make it a download on your blog site?  I'd even be willing to pay for it via PayPal.
Me too! :)
Saturday, May 8, 2010 12:26 AM
• Don't forget that a set of numbers may have more than one value for the mode.

http://mathworld.wolfram.com/Mode.html

So, (and I suspect there's a neater way of writing this):

Dim numbers() As Integer = {4, 4, 4, 4, 3, 2, 2, 2, 1, 1, 1, 1}

Dim modes = From a In (From n In numbers Group n By n Into g = Count() Select g, n) Order By a.g Where a.g = (From n In numbers Group n By n Into g = Count() Select g).Max Select a.n

' gives 4 and 1

Monday, May 10, 2010 10:20 AM
• Umm... the Order By got left in where it wasn't needed:

Dim modes = From a In _
(From n In numbers _
Group n By n Into g = Count() _
Select g, n) _
Where a.g = _
(From n In numbers _
Group n By n Into g = Count() Select g).Max _
Select a.n

Monday, May 10, 2010 10:36 AM
• Very kind of you, Frank and John!

Thanks!

www.insteptech.com ; msmvps.com/blogs/deborahk
Monday, May 10, 2010 2:43 PM
• Private Sub b_Capture_Click(ByVal sender As System.Object, ByVal e As System.Event.........)

Dim tamano As Integer

tamano=Me.DataGridView1.Rows.Count-2

Dim suma as Double

'forming the array from the DataGridView Input Control

Dim size(tamano) As String

For i As Integer =0 To tamano

size(i) = Me.DataGridView1.Item(0,1).value

Next

'Getting the Mean

Dim x As Integer=0

suma=0

For x=0 To tamano

suma=suma+size(x)

Next

'Display the mean to User

Me.tb_Promedio.Text=(suma/x).ToString

'Sort the array in ascending order

Dim temp(tamano) As String

For u=0 To size.Length

For y=u+1 To size.Length-1

if size(u)>size(y) Then

temp(u) = size(y)

size(u) = size(y)

size(y) = temp(u)

End If

Next

Next

'Getting the Median

Dim median As Integer

Dim data_size As Integer - tamano+1

If data_size Mod 2 =1 Then  'if 1 then odd else even

median= size((data_size-1)/2)

Else

median= size(data_size/2)

End If

'Displaying the Median to User

Me.tb_Median.Text= "The Median is: " + median.ToString

'Getting the Mode

Dim mode As Integer  =0

Dim count As Integer = 0

Dim countMax As Integer =0

For u=0 To size.Length-2

if size(u) = size(u+1) Then

count= count + 1

Else

if count > countMax Then

countMax = count

mode = size(u)

count = 0

End If

End If

Next

'Display the Mode

Me.tb_mode.Text = "The Mode is: " + mode.ToString

End Sub

Friday, October 12, 2012 6:42 PM
• Sosa F,

I have to point out that your calculation of the median is wrong when there is an even number of elements, and your code to find the mode fails when there is more than one mode.

For example, the median of {1,2,3,4} is 2.5, and the mode of {1,1,2,2} is {1,2}.

Also, this thread is more than two years old ;)

--
Andrew

Friday, October 12, 2012 7:07 PM
• Hi Andrew, Thanks for the corrections, I am new in programming and just wanted to help somebody who will be dealing with this topic in the future. The median for an even number of elements could be corrected with the folling modifications: Dim median As Decimal = 0 Dim data_size As Integer = tamano + 1 If data_size Mod 2 = 1 Then median = size((data_size-1)/2) Else median = (Cint(size(data_size/2)) + Cint(size(data_size/2 -1)))/2 En If 'Displaying the Median to user Me.tb_Median.Text = "The Median is: " + median.ToString Thanks a lot my friend and have an excellent weekend Peace & Good
Friday, October 12, 2012 9:10 PM