Question

Occassionally I need to check for duplicate IDs in a set of values and generally I use a Dictionary for this - using just the keys and leaving values empty.

Note that this is tight and highly optimized code so please no cries of 'premature optimization'! Assuming scenarios where CPU and RAM are being squeezed to the limit I was wanting to gather opinions on more optimal solutions; presumably something like a Lookup class would avoid unnecessary RAM allocations and would thus be slightly faster. Are there such classes either third party or perhaps some class I've overlooked in the BCL?

I understand google have released code for both fast and compact dictionary classes - perhaps there's something in there that could be ported to C#/.Net?

Thanks.

Was it helpful?

Solution

Use the HashSet class in .NET 3.5.

HashSet<int> set = new HashSet<int>() { 1, 2, 3 };
set.Add(5);
for (int index = 0; index < 10; index++)
{
    Console.WriteLine("{0} : {1}", index, set.Contains(index));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top