Results 1 to 9 of 9

Thread: Counting recurring elements in an array

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    5

    Counting recurring elements in an array

    Hi all,

    So i've got an array to hold names and I want to count how many times a name is held in the array. For example,

    John
    John
    James
    John
    James
    Jack
    Luke
    John
    Jack
    Luke

    If this was my array then I need a way to count how many times each name appears:
    John = 4
    James = 2
    Jack = 2
    Luke = 2

    Does anyone know of a way to do this? Its reallllyyyyy important!

    Regards,

    james

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Smile Re: Counting recurring elements in an array

    First add a reference to "Microsoft Scripting Runtime"
    then try the code (pass the array to the function)
    ...(found from google groups)

    VB Code:
    1. Sub CountDuplicates(A As Variant)
    2.  
    3.  
    4.     Dim D As Dictionary
    5.     Dim i As Long, key As Variant
    6.  
    7.  
    8.     Set D = New Dictionary
    9.     For i = LBound(A) To UBound(A)
    10.         key = A(i)
    11.         If D.Exists(key) Then
    12.             D.Item(key) = D.Item(key) + 1
    13.         Else
    14.             D.Add key, 1
    15.         End If
    16.     Next i
    17.     i = 0
    18.     For Each key In D.Keys
    19.          If key <> "" Then
    20.             MsgBox key & D.Item(key)
    21.          End If
    22.         i = i + 1
    23.     Next key
    24. End Sub

    Hope this helps!

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    5

    Re: Counting recurring elements in an array

    thanks for the quick reply but not really! i've got an array of names, some of which appear more than once in another position and i need to count how many times each name is repeated in the array.

    sorry for the misunderstanding

    james

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    5

    Re: Counting recurring elements in an array

    hey,

    the candidate names are stored in a 1-dimensional array called: candidate(0 To 5)
    The user is then presented with the names from the candidate array in the form of 6 drop down boxes. The user then selects their first choice and the clicks a button and the choice is saved into a 2-dimensional array called: choices(0 To 5, 0 To 19). 0 To 5 because there are 6 choices; 1st, 2nd, 3rd etc and 0 To 20 because there can be a maximum of 20 ballots.

    I need to be able to count how many times a candidate appears in first choice; i.e choices(0, x)

    Thanks guys for your help so far! I'm sure it can be done!

    james

  5. #5
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Question Re: Counting recurring elements in an array

    You have an array like this!!

    Dim arrNames(10) As String
    arrNames(0) = "AAA"
    arrNames(1) = "BBB"
    arrNames(2) = "BBB"
    arrNames(3) = "AAA"
    arrNames(4) = "AAA"
    arrNames(5) = "CCC"
    arrNames(6) = "DDD"
    arrNames(7) = "AAA"
    arrNames(8) = "BBB"
    arrNames(9) = "AAA"

    Right!!!

    Then call this function and see
    CountDuplicates arrNames

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    5

    Re: Counting recurring elements in an array

    ooooohh i see. You're a genious!
    Right i've created a third array and this array holds all the first choices from using this code

    VB Code:
    1. Dim i As Integer = 0
    2.         Dim arrNames(0 To 19) As String
    3.         For i = 0 To ballotnumber
    4.             arrNames(i) = choices(0, i)
    5.             i = i + 1
    6.         Next i

    But how do I count how many times a candidate occurs?

    We're getting closer....

    james

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Counting recurring elements in an array

    D.Item(key) holds the number of times key appears.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    5

    Re: Counting recurring elements in an array

    In this context, how could I get D.Item(key) to search through the array?

  9. #9
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Counting recurring elements in an array

    You have an array of six candidates by name: Candidate(5) As String
    You have an array of choices (20 ballots), also by name: Choices(5,19) As String

    Now then, the first array obviously needs to be an array of Strings, because that array contains the names of the candidates. The second array, however, does not need to be an array of Strings. It could instead be an array of Integer or Long variables - indexes to the Candidate() array. That is, instead of saying "My first choice is Bob", you could say "My first choice is [the second name in the array]". This makes life a lot easier.

    Most of the code below is only to create a sample set of choices to demonstrate the counting of those choices. The bit you need is indicated - a single line of code repeated in a nested loop of two variables.

    Once you understand the way I am counting votes here, you may be able to do away with the choices array altogether by counting the votes as they are cast. That is, if you don't need to know results of each individual ballot.

    VB Code:
    1. Private Sub Command1_Click()
    2.   Dim Candidate(5) As String ' The names of the candidates
    3.   Dim Choices(5, 19) As Long ' The choices, not as names, but as indexes to the Candidate() array
    4.   Dim CountChoices(5, 5) As Long ' This will make it very easy to count the votes
    5.   Dim i As Long, j As Long ' loop counters/array index variables
    6.   Dim r As Long, t As Long ' two variables used to generate random ballots
    7.  
    8.   ' A sample group of candidates
    9.   Candidate(0) = "Adam"
    10.   Candidate(1) = "Bob"
    11.   Candidate(2) = "Chad"
    12.   Candidate(3) = "Dave"
    13.   Candidate(4) = "Eric"
    14.   Candidate(5) = "Fred"
    15.  
    16.   ' These two loops create a randomized selection of ballots
    17.   ' Initialize the choices, so that each candidate appears once on each ballot
    18.   For i = 0 To 5
    19.     For j = 0 To 19
    20.       Choices(i, j) = i
    21.   Next j, i
    22.  
    23.   ' Randomize the order in which the candidates are chosen on each ballot
    24.   For j = 0 To 19
    25.     For i = 5 To 1 Step -1
    26.       r = Int(Rnd * (i + 1))
    27.       t = Choices(i, j)
    28.       Choices(i, j) = Choices(r, j)
    29.       Choices(r, j) = t
    30.     Next i
    31.   Next j
    32.  
    33.   ' Display the results in Debug window
    34.   ' First row is first choices, second row is second choice, etc
    35.   For i = 0 To 5
    36.     For j = 0 To 19
    37.       Debug.Print Choices(i, j);
    38.     Next j
    39.     Debug.Print
    40.   Next i
    41.  
    42.   ' This is the part you need: how to count those votes
    43.   For j = 0 To 19 ' Loop through each ballot
    44.     For i = 0 To 5 ' Loop through each choice
    45.       CountChoices(i, Choices(i, j)) = CountChoices(i, Choices(i, j)) + 1
    46.     Next i
    47.   Next j
    48.  
    49.   ' Display the counts
    50.   For i = 0 To 5
    51.     Debug.Print
    52.     Select Case i
    53.     Case 0: Debug.Print "First choice:"
    54.     Case 1: Debug.Print "Second choice:"
    55.     Case 2: Debug.Print "Third choice:"
    56.     Case 3: Debug.Print "Fourth choice:"
    57.     Case 4: Debug.Print "Fifth choice:"
    58.     Case 5: Debug.Print "Sixth choice:"
    59.     End Select
    60.     For j = 0 To 5
    61.       Debug.Print Space(3); Candidate(j); CountChoices(i, j)
    62.     Next j
    63.   Next i
    64. End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width