Click to See Complete Forum and Search --> : deleting duplicate items from collection?


Muthu Ram
October 1st, 1999, 06:02 PM
how to delete or find out duplicate items from a collection. i mean looking for a fast alogorithm which can do this.. or do you know how to catch the error when a duplicate key is added to a collection?

Lothar Haensler
October 4th, 1999, 02:02 AM
VB does not allow duplicates in a collection if you use the Key argument to the Add method of the collection object.
Simply trap the runtime error as in

dim col as new collection
on error goto erradd
col.add Key:=mykey, Item:=myItem
exit sub
erradd:
' handle error here

Muthu Ram
October 4th, 1999, 10:05 AM
i used this when i put in the key, but when not using the key there would be duplicates in the collection. i guess you got to write your own collection and then check the item you add before putting them in the collection.

czimmerman
October 4th, 1999, 12:47 PM
See http://www.freevbcode.com/ShowCode.Asp?ID=40.
This is a function that evaluates a list (array or collection) and returns false if it does not contain unique values. You can easily modify it to return a collection of unique values instead.

Lothar Haensler
October 5th, 1999, 03:34 AM
you might even be better off by using the
Dictionary object in VB 6!
it's faster and has more methods...