array finding

hi i need to find from in an array how many of each number there are eg array = 1,1,1,2,3,3

now how do i get it to find those and then determine how many one's there are and how many two's there are and how many three's there are



Answer this question

array finding

  • Michael J Brown

    ok but how do i use it. how do i get from that bit of code the number in the array and how many of them there are. so number 1 and there is 3 of them.
  • Gurpreet Singh Gill

    This should work, a few minor changes.

    Public Class Form1


        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim x As New Dictionary(Of Integer, Integer) '//Create a dictionary
            Dim MyNumbers As Integer() = {1, 1, 1, 1, 2, 3, 3, 3, 4} ' Our numbers

            x = BrainTickler(MyNumbers) '//Call the method to total them

            '//Build a string of the results
            Dim strContents As String = ""
            For Each a As KeyValuePair(Of Integer, Integer) In x
                strContents = strContents & a.Key.ToString & vbTab & a.Value.ToString & vbCrLf
            Next

            '//Display the results
            MsgBox(strContents)

        End Sub


        Private Function BrainTickler(ByVal MyNumbers As Integer()) As Dictionary(Of Integer, Integer)

            Dim MyCounts As New Dictionary(Of Integer, Integer) ' Keeps track of our counts
            Dim I As Integer
            For I = 0 To MyNumbers.Length - 1
                If MyCounts.ContainsKey(MyNumbers(I)) = False Then ' Check to see if the number exists
                    MyCounts.Add(MyNumbers(I), 1) ' Add key to count and set count to 1 since we just found it
                Else
                    MyCounts(MyNumbers(I)) = MyCounts(MyNumbers(I)) + 1    ' Increment the counter
                End If
            Next
            Return MyCounts
        End Function
    End Class


  • Peter Rook

    Here is one cute way of doing it. And the explanation of how it works.
    We have a dictionary which basically can contain a key. Each key is used to lookup a value. In this case we are going to use the numbers found in our array (MyNumbers) as the key. The values that will be looked up will be the count.
    So we declare our variables and then we go through our for loop.
    For each number in MyNumbers we check to see if this number exists as a key in our Counter array. If it doesn't we add it and set it's count to 1 since we found one occurrance. If it already exists we simply increment the counter.
    So first pass we have a 1 and we see if it's in MyCounts -which i'ts not so we add it . Second pass we find another one and see if it is in which it is so we simply incremement that. Repeat that for 3rd and 4th pass and soon we have 4 1's . etc

    Private Function BrainTickler() As Dictionary(Of Int32, Integer)
    Dim MyNumbers As Integer() = {1, 1, 1, 1, 2, 3, 3, 3, 4} ' Our numbers
    Dim MyCounts As New Dictionary(Of Int32, Integer) ' Keeps track of our counts
    Dim I As Int32
    For I = 0 To MyNumbers.Length - 1
    If MyCounts.ContainsKey(MyNumbers(I)) = False Then ' Check to see if the number exists
    MyCounts.Add(MyNumbers(I), 1) ' Add key to count and set count to 1 since we just found it
    Else
    MyCounts(MyNumbers(I)) = MyCounts(MyNumbers(I)) + 1 ' Increment the counter
    End If
    Next
    BrainTickler = MyCounts
    Dim P As Int32
    End Function

    Hope that helps.

  • array finding