|
-
Feb 14th, 2007, 07:44 AM
#1
Thread Starter
New Member
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
-
Feb 14th, 2007, 08:44 AM
#2
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:
Sub CountDuplicates(A As Variant)
Dim D As Dictionary
Dim i As Long, key As Variant
Set D = New Dictionary
For i = LBound(A) To UBound(A)
key = A(i)
If D.Exists(key) Then
D.Item(key) = D.Item(key) + 1
Else
D.Add key, 1
End If
Next i
i = 0
For Each key In D.Keys
If key <> "" Then
MsgBox key & D.Item(key)
End If
i = i + 1
Next key
End Sub
Hope this helps!
-
Feb 14th, 2007, 09:18 AM
#3
Thread Starter
New Member
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
-
Feb 14th, 2007, 09:37 AM
#4
Thread Starter
New Member
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
-
Feb 14th, 2007, 09:39 AM
#5
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
-
Feb 14th, 2007, 09:52 AM
#6
Thread Starter
New Member
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:
Dim i As Integer = 0
Dim arrNames(0 To 19) As String
For i = 0 To ballotnumber
arrNames(i) = choices(0, i)
i = i + 1
Next i
But how do I count how many times a candidate occurs?
We're getting closer....
james
-
Feb 14th, 2007, 10:40 AM
#7
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
-
Feb 14th, 2007, 10:51 AM
#8
Thread Starter
New Member
Re: Counting recurring elements in an array
In this context, how could I get D.Item(key) to search through the array?
-
Feb 14th, 2007, 10:57 PM
#9
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:
Private Sub Command1_Click()
Dim Candidate(5) As String ' The names of the candidates
Dim Choices(5, 19) As Long ' The choices, not as names, but as indexes to the Candidate() array
Dim CountChoices(5, 5) As Long ' This will make it very easy to count the votes
Dim i As Long, j As Long ' loop counters/array index variables
Dim r As Long, t As Long ' two variables used to generate random ballots
' A sample group of candidates
Candidate(0) = "Adam"
Candidate(1) = "Bob"
Candidate(2) = "Chad"
Candidate(3) = "Dave"
Candidate(4) = "Eric"
Candidate(5) = "Fred"
' These two loops create a randomized selection of ballots
' Initialize the choices, so that each candidate appears once on each ballot
For i = 0 To 5
For j = 0 To 19
Choices(i, j) = i
Next j, i
' Randomize the order in which the candidates are chosen on each ballot
For j = 0 To 19
For i = 5 To 1 Step -1
r = Int(Rnd * (i + 1))
t = Choices(i, j)
Choices(i, j) = Choices(r, j)
Choices(r, j) = t
Next i
Next j
' Display the results in Debug window
' First row is first choices, second row is second choice, etc
For i = 0 To 5
For j = 0 To 19
Debug.Print Choices(i, j);
Next j
Debug.Print
Next i
' This is the part you need: how to count those votes
For j = 0 To 19 ' Loop through each ballot
For i = 0 To 5 ' Loop through each choice
CountChoices(i, Choices(i, j)) = CountChoices(i, Choices(i, j)) + 1
Next i
Next j
' Display the counts
For i = 0 To 5
Debug.Print
Select Case i
Case 0: Debug.Print "First choice:"
Case 1: Debug.Print "Second choice:"
Case 2: Debug.Print "Third choice:"
Case 3: Debug.Print "Fourth choice:"
Case 4: Debug.Print "Fifth choice:"
Case 5: Debug.Print "Sixth choice:"
End Select
For j = 0 To 5
Debug.Print Space(3); Candidate(j); CountChoices(i, j)
Next j
Next i
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|