Better structure for Looking up booleans for an index?

This is a bit nitpicking and I might have overlooked something simple

Sometimes I have some integers and have to be able to lookup whether they're true or false. I've always solved this by using an Dictionary<int,bool> , inserting a true and just using ContainsKey to see if something is true (it's false by default.)

But it doesn't seem like an elegant solution to insert a bool that isn't even used :) What's structure would you recommend for this? Lookup performance being primary and insertions secondary (but important)

Some nice (LINQ) syntax for having a bunch of indices and returning items that are not contained would be a bonus

Answers


Use a HashSet<int> to hold the "true" integers and use Contains to search the collection. With this approach you store just the data you need and you get the same O(1) performance you are currently getting with the dictionary.


Need Your Help

Replace all email addresses after @ in Sql

sql-server tsql

I have a list of emails. I want to change all of them to test emails for my test system e.g. John@gmail.com to john@test.com.

How to display toolbar/pager in place of breadcrumbs at the top in magento category view page?

magento magento-1.7

I want to display the toolbar/pager in place of breadcrumbs, I have removed breadcrumbs, but I am not able to display the toolbar/pager in place of breadcrumbs. How to do that? I've tried putting xml

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.