How to find duplicate data in an array

C

Charlie

I have a string arrary Schedule(Names, Info)
There are about 100 names in the array, but some of them are duplicates.
How can I debug.print each Name, then the info that goes with it, and if it
has a duplicate Name in the array, the info from that part also. The array
is not sort in any way
thanks,
ck
 
P

Peter Jamieson

Something like the following, perhaps?

Sub debugprintdups()
Dim Schedule(100, 2) As String
Dim strResult() As String
Dim i As Long
Dim j As Long
' some sample data
Schedule(1, 1) = "name1"
Schedule(2, 1) = "name2"
Schedule(3, 1) = "name3"
Schedule(10, 1) = "name1"
Schedule(11, 1) = "name1"
Schedule(19, 1) = "name1"
Schedule(1, 2) = "info1"
Schedule(10, 2) = "info2"
Schedule(11, 2) = "info3"
Schedule(19, 2) = "info4"
Schedule(2, 2) = "info5"
Schedule(3, 2) = "info6"

' Create our array of results
ReDim strResult(LBound(Schedule, 1) To UBound(Schedule, 1))
For i = LBound(Schedule, 1) To UBound(Schedule, 1)
For j = LBound(Schedule, 1) To i - 1
If Schedule(j, 1) = Schedule(i, 1) Then Exit For
Next
If j = i Then
strResult(i) = Schedule(i, 1) & " " & Schedule(i, 2)
Else
strResult(j) = strResult(j) & " " & Schedule(i, 2)
End If
Next

For i = LBound(strResult, 1) To UBound(strResult, 1)
If strResult(i) <> "" Then
Debug.Print strResult(i)
End If
Next

End Sub

Peter Jamieson
 
C

Cliff Carson

I think a fairly simple method to implement and verify for correctness would
be to create a Dictionary object and add each item in your array using Name
as the key and Info as the item value. If you get a key collision, which
you can check in advance with Dictionary.Exists, delete the key item pair
and add it back in with the item value concatenated to the previous item
value separated by a unique delimiter. Since info appears to be printable
you can use something non-printable for the delimiter such as chr(1). Then
iterate through the Dictionary object and print your key value and possibly
multiple item values.

CC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top