View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
XP XP is offline
external usenet poster
 
Posts: 389
Default Speed up Array Transfer to Dictionary


Just a thought,

How about you load two arrays. One unique that you will load into the
dictionary and the second to just count the dupes? Or just count the dupes in
the source directly...?

I think that might run faster since there will be fewer comparisons?

"ExcelMonkey" wrote:

Hi I have a routine which checks an array with duplicate entries and tranfers
only the unique items to a dictionary. The code works fine excpet it is very
slow. Its slow because the array has to compare each of its element to every
other element in the array. If the size of the array is large, the code
slows down dramatically.

The array is called PR2 and the Dictionary is called Dict1.

Note I need all the duplicates as I use them to calculate a frequency of
occurence for each duplicate. This is why I kept the duplicate data in an
array in the first place as a dictionary cannot have duplicate keys.

Set Dict1 = New Dictionary

For t = LBound(PR2) To UBound(PR2)
If Not Dict1.Exists(PR2(t)) Then
For z = LBound(PR2) To UBound(PR2)
If z = t Then
'Do nothing as you are comparing this
'word to itself
ElseIf z < UBound(PR2) Then
If PR2(t) = PR2(z) Then
'Duplicate flagged. Don't add to
'Dictionary
dupe = dupe + 1
End If
Else
'Only add to dictionary on last
'run as dictionary can not have duplicate
'keys
Dict1.Add PR2(t), dupe
End If
Next
End If
'Reset Duplicate
dupe = 1
Next

Thanks

EM