Stern said:
Hello,
I have a Vector of objects, each of them having a name. Now I need to
find in this Vector objects with the same names, get the list of them
and remove them from the vector. I don't know which names these are.
Is there any quick solution for the problem?
I don't think there's a quick way, unless your pre-index your vector so that
you already have the objects indexed by name (e.g. a Map that maps a name to
a list of those items that share that name.) Then you just drop the lists
that have > 1 item.
To do it on-the-fly, create the following data structures:
1) a hash map "uniqueNames" that will map a string to an item for an item
that has been seen for the first time.
2) a set "multiNames" that will contain the names of items that have been
seen more than once
3) a list "deleteItems" that will contain items to be deleted.
For each item in the vector {
if the item's name is in the multiNames set,
add the item to the deleteItems list.
else if the item's name is a key in the uniqueNames hash map,
add the item to the deleteItems list
remove the item name from the uniqueNames hash table
add the item's name ot the multiNames set
else // the item has never been seen
add the name / item to the uniqueNames hash set
}
Walk the deleteItems list to delete the items.
If you have to do this alot I would really recommend just keeping a
name-based index to the list of shared items as outlined above.
Cheers,
Matt Humphrey (e-mail address removed)
http://www.iviz.com/