RobVious RobVious - 1 year ago 74
C# Question

Searching for a string in HashSet<string> Performance

I have a

with ~50k members. I have another list of objects that I'm iterating through one by one to determine if the object's email exists. If it does, I need to perform some action on the object.

var emailList = db.Emails.Select(s => s.EmailAddress.ToLower()).ToList();
var emailHash = new HashSet<string>(emailList);
var objects = db.Objects.ToList();
// everything is fine up to this point
foreach (var object in objects) {
if (!emailHash.Any(s => s.Equals(object.Email))) { // This takes ~0.3s
Console.WriteLine("Email: {0}", object.Email);

What can I do to speed up the evaluation of whether or not one string exists in a list of strings?

Answer Source

You are not using the HashSet correctly. Using Linq's .Any() will actually evaluate your condition against each element stored in the HashSet.

To search if an item exists in a HashSet (with constant time, O(1)) use emailHash.Contains(object.Email).