Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2006

Tip: Looking for answers? Try searching our database.

How to find duplicate data in an array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Charlie - 13 Feb 2006 13:13 GMT
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
Peter Jamieson - 13 Feb 2006 14:17 GMT
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

>I have a string arrary Schedule(Names, Info)
> There are about 100 names in the array, but some of them are duplicates.
[quoted text clipped - 5 lines]
> thanks,
> ck
Cliff Carson - 13 Feb 2006 17:40 GMT
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

> I have a string arrary Schedule(Names, Info)
> There are about 100 names in the array, but some of them are duplicates.
[quoted text clipped - 3 lines]
> thanks,
> ck

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.