Is there a way to count the number of unique instances of items in an ArrayList without iterating through the entire list? Here is my code, and I would like to count the number of instances of Orange without iterating through the entire list - I am hoping there is a method to do it? In practice, the actual ArrayList will have hundreds of thousands of items.
Private Sub TestArrayList2()
Dim TestAR As Object
Dim Count As Integer
Set TestAR = CreateObject("System.Collections.ArrayList")
TestAR.Add "Apple"
TestAR.Add "Orange"
TestAR.Add "Orange"
TestAR.Add "Orange"
TestAR.Add "Pear"
Count = TestAR.Count
End Sub
I can't find a built in single function, but once the array list is sorted it's a simple case of locating the first (.IndexOf
) and last (.lastIndexOf
) occurrence of the string you're looking for:
lookingfor = "Orange"
TestAR.Sort
occurrences = TestAR.lastIndexOf(lookingfor) - TestAR.IndexOf(lookingfor, 0) + 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With