I have a class that has a dictionary that stores string values and a list that stores unsigned 64-bit integers. This class is considered equal to another class of the same type if their dictionaries contain the same keys and values.
I also have a static list containing several instances of this class. I want to check if there are duplicate items in this list. If there are duplicates, I want to merge their lists.
Here’s a pseudo-code I thought:
foreach (var item in StaticList)
{
if (item.Equals(anotherItem)) // i.e., dictionaries are equal
{
item.UInt64List.AddRange(anotherItem.UInt64List);
StaticList.Remove(anotherItem);
}
}
How can I accomplish this without resorting to features added by the latest frameworks (my target framework is .NET 2.0, so there’s no fancy Union, Intersect, etc.)?
Note: I have already figured out how to check if two dictionaries are equal, I want to find duplicates and merge them.
You have to compare all the items against each other.
I do not recommend using a
foreachloop, because you cannot modify the list during iteration and you have to put items in another list and remove them later, which imposes a lot of performance penalty to store items and to search later to remove.