# count the number of occurences of an element in an array

### Question

• i want to count the number of occurences of an element that are there in a 2d array. below is my code
Dim numbers(,) As Integer = {{1, 2}, _
{4, 5}, _
{7, 9}, _
{3, 1}, _
{2, 3}, _
{5, 9}, _
{8, 8}, _
{9, 9}, _
{7, 3}, _
{2, 1}, _
{5, 4}}

Dim arrC(9) As Integer '= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Dim rows As Integer
Dim columns As Integer
Dim i, counter, totalrows As Integer
'totalrows = ((numbers.Length / 2) - 1)
For i = 0 To 10
For j = 0 To 1
If arrC(numbers(i, j)) Then
MessageBox.Show(arrC(numbers(i, j)).ToString)
arrC(numbers(i, j)) += 1

Else
arrC(numbers(i, j)) = 1
End If
Next
Next
For Each a_arrc In arrC
MessageBox.Show(a_arrc.ToString)
Next

Sunday, December 07, 2008 1:31 AM

• Hi GJ111,

Welcome to the forums. :-)

Try a FOR EACH loop like this.

Try this code with one button on a FORM please.

Please note you said; "an element" not that you want to count every element occurrence.

Result = There are 3 number 5's

Regards,

John

 Option Strict 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 = _ {{1, 2}, {4, 5}, {7, 9}, {3, 1}, {2, 3}, {5, 9}, _ {8, 8}, {9, 9}, {7, 3}, {2, 1}, {5, 4}} 'To count the number of number 5's in the array called "numbers" Dim count As Integer = 0 For Each num As Integer In numbers If num = 5 Then count = count + 1 Next MessageBox.Show("There are " & count.ToString & " number 5's") End Sub End Class

Sunday, December 07, 2008 3:13 AM
• Hi again,

If you want to count each and every number then do this.

Regards,

John

 Option Strict 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 = _ {{1, 2}, {4, 5}, {7, 9}, {3, 1}, {2, 3}, {5, 9}, _ {8, 8}, {9, 9}, {7, 3}, {2, 1}, {5, 4}} Dim counts(9) As Integer For Each num As Integer In numbers Select Case num Case 0 counts(0) += 1 Case 1 counts(1) += 1 Case 2 counts(2) += 1 Case 3 counts(3) += 1 Case 4 counts(4) += 1 Case 5 counts(5) += 1 Case 6 counts(6) += 1 Case 7 counts(7) += 1 Case 8 counts(8) += 1 Case 9 counts(9) += 1 End Select Next Dim sb As New System.Text.StringBuilder For index As Integer = 0 To 9 sb.Append("There are " & counts(index).ToString & " number " & index.ToString & "'s found." & ControlChars.NewLine) Next MessageBox.Show(sb.ToString) End Sub End Class

Sunday, December 07, 2008 3:28 AM
• Hi,
John's answer is quite good for the question. And I just do a little modification in the following way:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim numbers(,) As Integer = _
{{1, 2}, {4, 5}, {7, 9}, {3, 1}, {2, 3}, {5, 9}, _
{8, 8}, {9, 9}, {7, 3}, {2, 1}, {5, 4}}

Dim counts(9) As Integer
For Each num As Integer In numbers
If num >= 0 And num < 10 Then
counts(num) += 1
End If
Next

Dim sb As New System.Text.StringBuilder
For index As Integer = 0 To 9
sb.Append(index.ToString & ":  " & counts(index).ToString & ControlChars.NewLine)
Next
MessageBox.Show(sb.ToString)

End Sub
End Class

Monday, December 08, 2008 9:39 AM
• this will work with any integers in numbers.  if the range of numbers is large another approach would be better.

 Dim numbers(,) As Integer = {{1, 2}, _ {4, 5}, _ {7, 9}, _ {3, 1}, _ {2, 3}, _ {5, 9}, _ {8, 8}, _ {9, 9}, _ {7, 3}, _ {2, 1}, _ {13, 20}, _ {19, 21}, _ {5, 4}} Dim arrC As New List(Of Integer) Dim nRow As Integer, nCol As Integer For nRow = 0 To numbers.GetUpperBound(0) For nCol = 0 To numbers.Rank - 1 If numbers(nRow, nCol) >= arrC.Count Then Do While numbers(nRow, nCol) >= arrC.Count arrC.Add(0) Loop End If arrC(numbers(nRow, nCol)) += 1 Next Next Dim sb As New System.Text.StringBuilder For i As Integer = 0 To arrC.Count - 1 sb.Append(i.ToString.PadRight(8, " "c)) sb.Append(arrC(i).ToString) sb.Append(Environment.NewLine) Next MessageBox.Show(sb.ToString) Stop

Monday, December 08, 2008 11:58 AM
• If you're using VB2008, then LINQ and Llamda functions come in very handy:

 Public Class Program Public Shared Sub Main() Dim vehicles() As Vehicle = { _ New Vehicle("Car", "Red", "Ferrari"), _ New Vehicle("Truck", "Green", "Ford"), _ New Vehicle("Truck", "White", "Chevy"), _ New Vehicle("Car", "Blue", "Aston Martin"), _ New Vehicle("Car", "Red", "Porsche"), _ New Vehicle("Truck", "Brown", "Toyota"), _ New Vehicle("Car", "Orange", "Honda"), _ New Vehicle("Car", "Blue", "Toyota"), _ New Vehicle("Truck", "Orange", "Dodge") _ } 'using Llambda functions Dim redFerraris = vehicles.Where(Function(v) v.Make = "Ferrari" AndAlso v.Color = "Red") Dim blueCars = vehicles.Where(Function(v) v.Type = "Car" AndAlso v.Color = "Blue") 'using LINQ        Dim trucks = From  v In  vehicles _                     Where v.Type = "Truck" _                     Select  v        Dim linqRedFerraris = From v1 In vehicles _                        Where v1.Make = "Ferrari" AndAlso v1.Color = "Red" _                        Select v1 Console.ReadLine() End Sub End Class Public Class Vehicle Public Type As String Public Color As String Public Make As String Public Sub New() End Sub Public Sub New(ByVal t As String, ByVal c As String, ByVal m As String) Type = t Color = c Make = m End Sub End Class

I used a simple class for Vehicle with nothing but public string fields for the example, to keep it short.

This same sort of thing can be accomplished in VB2005 by using the Array.FindAll method in conjunction with a delegate.

Chris
• Edited by Tuesday, December 09, 2008 8:42 PM Added additional code
• Proposed as answer by Tuesday, December 09, 2008 9:41 PM
• Marked as answer by Friday, December 12, 2008 7:41 AM
Tuesday, December 09, 2008 8:35 PM

### All replies

• Hi GJ111,

Welcome to the forums. :-)

Try a FOR EACH loop like this.

Try this code with one button on a FORM please.

Please note you said; "an element" not that you want to count every element occurrence.

Result = There are 3 number 5's

Regards,

John

 Option Strict 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 = _ {{1, 2}, {4, 5}, {7, 9}, {3, 1}, {2, 3}, {5, 9}, _ {8, 8}, {9, 9}, {7, 3}, {2, 1}, {5, 4}} 'To count the number of number 5's in the array called "numbers" Dim count As Integer = 0 For Each num As Integer In numbers If num = 5 Then count = count + 1 Next MessageBox.Show("There are " & count.ToString & " number 5's") End Sub End Class

Sunday, December 07, 2008 3:13 AM
• Hi again,

If you want to count each and every number then do this.

Regards,

John

 Option Strict 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 = _ {{1, 2}, {4, 5}, {7, 9}, {3, 1}, {2, 3}, {5, 9}, _ {8, 8}, {9, 9}, {7, 3}, {2, 1}, {5, 4}} Dim counts(9) As Integer For Each num As Integer In numbers Select Case num Case 0 counts(0) += 1 Case 1 counts(1) += 1 Case 2 counts(2) += 1 Case 3 counts(3) += 1 Case 4 counts(4) += 1 Case 5 counts(5) += 1 Case 6 counts(6) += 1 Case 7 counts(7) += 1 Case 8 counts(8) += 1 Case 9 counts(9) += 1 End Select Next Dim sb As New System.Text.StringBuilder For index As Integer = 0 To 9 sb.Append("There are " & counts(index).ToString & " number " & index.ToString & "'s found." & ControlChars.NewLine) Next MessageBox.Show(sb.ToString) End Sub End Class

Sunday, December 07, 2008 3:28 AM
• Hi,
John's answer is quite good for the question. And I just do a little modification in the following way:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim numbers(,) As Integer = _
{{1, 2}, {4, 5}, {7, 9}, {3, 1}, {2, 3}, {5, 9}, _
{8, 8}, {9, 9}, {7, 3}, {2, 1}, {5, 4}}

Dim counts(9) As Integer
For Each num As Integer In numbers
If num >= 0 And num < 10 Then
counts(num) += 1
End If
Next

Dim sb As New System.Text.StringBuilder
For index As Integer = 0 To 9
sb.Append(index.ToString & ":  " & counts(index).ToString & ControlChars.NewLine)
Next
MessageBox.Show(sb.ToString)

End Sub
End Class

Monday, December 08, 2008 9:39 AM
• this will work with any integers in numbers.  if the range of numbers is large another approach would be better.

 Dim numbers(,) As Integer = {{1, 2}, _ {4, 5}, _ {7, 9}, _ {3, 1}, _ {2, 3}, _ {5, 9}, _ {8, 8}, _ {9, 9}, _ {7, 3}, _ {2, 1}, _ {13, 20}, _ {19, 21}, _ {5, 4}} Dim arrC As New List(Of Integer) Dim nRow As Integer, nCol As Integer For nRow = 0 To numbers.GetUpperBound(0) For nCol = 0 To numbers.Rank - 1 If numbers(nRow, nCol) >= arrC.Count Then Do While numbers(nRow, nCol) >= arrC.Count arrC.Add(0) Loop End If arrC(numbers(nRow, nCol)) += 1 Next Next Dim sb As New System.Text.StringBuilder For i As Integer = 0 To arrC.Count - 1 sb.Append(i.ToString.PadRight(8, " "c)) sb.Append(arrC(i).ToString) sb.Append(Environment.NewLine) Next MessageBox.Show(sb.ToString) Stop

Monday, December 08, 2008 11:58 AM
• Hi ALL,

I thought I would add an updated version.  :-)    ;-)

By the way thanks typot for your idea.  :-)

This version does not show any value where the count is zero.

I thought what is the point in showing a zero count?

It will also only work for INTEGER values greater than or equal to zero so the array is declared as

UInteger which means an Unsigned Integer.

Again this code has a limited range depending on what the highest number in the array of numbers is.

Regards,

John

 Option Strict On Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim numbers(,) As UInteger = _ {{1, 2}, {4, 5}, {765, 9}, {3, 1}, {2, 3}, {5, 9}, _ {8, 8}, {9, 9}, {765, 3}, {2, 1}, {5, 1000}} Dim numbersList As New List(Of UInteger) For Each num As UInteger In numbers numbersList.Add(num) Next Try Dim newArray() As UInteger = numbersList.ToArray Array.Sort(newArray) Dim counts(CInt(newArray(newArray.GetUpperBound(0)))) As Integer For Each num As UInteger In newArray If num >= 0 And num <= newArray(newArray.GetUpperBound(0)) Then counts(CInt(num)) += 1 End If Next Dim sb As New System.Text.StringBuilder For index As Integer = CInt(newArray(newArray.GetLowerBound(0))) To CInt(newArray(newArray.GetUpperBound(0))) If counts(index) <> 0 Then sb.Append(index.ToString & ":  " & counts(index).ToString & ControlChars.NewLine) End If Next MessageBox.Show(sb.ToString) Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub End Class

Tuesday, December 09, 2008 1:14 PM
• Hi ALL,

Here is an INTEGER version of the above code but a rethink would be need as the -999 is not detected.

This is because negative array indexes are not allowed in Vb.Net.

Regards,

John

 Option Strict 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 = _ {{1, 2}, {4, 5}, {765, -999}, {3, 1}, {2, 3}, {5, 9}, _ {8, 8}, {9, 9}, {765, 3}, {2, 1}, {5, 1000}} Dim numbersList As New List(Of Integer) For Each num As Integer In numbers numbersList.Add(num) Next Try Dim newArray() As Integer = numbersList.ToArray Array.Sort(newArray) Dim counts(CInt(newArray(newArray.GetUpperBound(0)))) As Integer For Each num As Integer In newArray If num >= 0 And num <= newArray(newArray.GetUpperBound(0)) Then counts(CInt(num)) += 1 End If Next Dim sb As New System.Text.StringBuilder For index As Integer = 0 To CInt(newArray(newArray.GetUpperBound(0))) If counts(index) <> 0 Then sb.Append(index.ToString & ":  " & counts(index).ToString & ControlChars.NewLine) End If Next MessageBox.Show(sb.ToString) Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub End Class

Tuesday, December 09, 2008 1:35 PM
• Hi ALL,

So what I mention above leads me to another interesting problem.

How would you count the occurrence of each of the various OBJECTs in an array as well count the occurrence of each of the numbers even if they are positive or negative?

1) Say we count the occurrence of each of the numbers that are zero and above, now we want to count the occurrence of each of the numbers below zero as well as count the occurrence of each of the other objects that are identical. How could we do that?

Say we have 2 Red Ferrari cars and lots of other cars, how could we count the objects of TYPE = CAR or say count the number of RED cars or say count the number of Ferrari cars.

Now you might be thinking this is erring on databases and SQL queries and you would be right.

2) For a second but similar problem, I am thinking of 1 general routine to count each number whether positive or negative as well as simply count the objects of a particular TYPE, such as count the STRINGs, and count each occurence of each number too.

I guess both problems would possibly be solved in
conjuction with a COMPARER and a DICTIONARY maybe?

Regards,

John

Tuesday, December 09, 2008 2:24 PM
• If you're using VB2008, then LINQ and Llamda functions come in very handy:

 Public Class Program Public Shared Sub Main() Dim vehicles() As Vehicle = { _ New Vehicle("Car", "Red", "Ferrari"), _ New Vehicle("Truck", "Green", "Ford"), _ New Vehicle("Truck", "White", "Chevy"), _ New Vehicle("Car", "Blue", "Aston Martin"), _ New Vehicle("Car", "Red", "Porsche"), _ New Vehicle("Truck", "Brown", "Toyota"), _ New Vehicle("Car", "Orange", "Honda"), _ New Vehicle("Car", "Blue", "Toyota"), _ New Vehicle("Truck", "Orange", "Dodge") _ } 'using Llambda functions Dim redFerraris = vehicles.Where(Function(v) v.Make = "Ferrari" AndAlso v.Color = "Red") Dim blueCars = vehicles.Where(Function(v) v.Type = "Car" AndAlso v.Color = "Blue") 'using LINQ        Dim trucks = From  v In  vehicles _                     Where v.Type = "Truck" _                     Select  v        Dim linqRedFerraris = From v1 In vehicles _                        Where v1.Make = "Ferrari" AndAlso v1.Color = "Red" _                        Select v1 Console.ReadLine() End Sub End Class Public Class Vehicle Public Type As String Public Color As String Public Make As String Public Sub New() End Sub Public Sub New(ByVal t As String, ByVal c As String, ByVal m As String) Type = t Color = c Make = m End Sub End Class

I used a simple class for Vehicle with nothing but public string fields for the example, to keep it short.

This same sort of thing can be accomplished in VB2005 by using the Array.FindAll method in conjunction with a delegate.

Chris
• Edited by Tuesday, December 09, 2008 8:42 PM Added additional code
• Proposed as answer by Tuesday, December 09, 2008 9:41 PM
• Marked as answer by Friday, December 12, 2008 7:41 AM
Tuesday, December 09, 2008 8:35 PM
• findall only works for one dimensional arrays.
Tuesday, December 09, 2008 9:43 PM
•
Chris Dunaway said:

If you're using VB2008, then LINQ and Llamda functions come in very handy:

I used a simple class for Vehicle with nothing but public string fields for the example, to keep it short.

This same sort of thing can be accomplished in VB2005 by using the Array.FindAll method in conjunction with a delegate.

Chris

Hi Chris,

LINQ and Llamda functions are some of the other things I need to look into and learn.

I have already done SQL at university recently on my current course so I can see the SQL style statements in your code.

Thanks again. :-)

Regards,

John