Q:

Counting occurrences of strings in an Array

I am basically pasting this from another forum where I saw it asked (with nobody answering).  It is essentially the exact same thing im trying to do:
In ASP classic, is there a way to count the number of times a string appears in an array of strings and output them based on string and occurrence count?
For example if I have an array which contains the following :
hello
happy
hello
hello
testing
hello
test
happy
The output would be:
hello 4
happy 2
test 1
testing 1
Thanks!

A:

I'm assuming the language is VBScript (since that's what most people use with classic ASP).
You can use a Dictionary object to keep track of the individual counts:
Function CountValues(pArray)
    Dim i, item
    Dim dictCounts
    Set dictCounts = Server.CreateObject("Scripting.Dictionary")

    For i = LBound(pArray) To UBound(pArray)
        item = pArray(i)
        If Not dictCounts.Exists(item) Then
            dictCounts.Add item, 0
        End If
        dictCounts.Item(item) = dictCounts.Item(item) + 1
    Next

    Set CountValues = dictCounts
End Function

