Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to remove all entries in a dictionary lower than a specified value

I need to remove all entries in a dictionary accordingly to a specified lower bound.

My current solution is this:

    List<string> keys = new List<string>();

    foreach (KeyValuePair<string, int> kvp in dic)
    {
        if (kvp.Value < lowerBound)
            keys.Add(kvp.Key);
    }

    foreach (string key in keys)
        dic.Remove(key);

However this is rather expensive, especially since the size of the dictionary is rather large.

I've seen a LINQ solution like:

foreach(var kvp in dic.Where(kvp.Value <= lowerBound).ToDictionary())
{
    dic.Remove(kvp.Key);
}

which I assume to be better since it's just 1 foreach, but I'm getting:

The name 'kvp' does not exist in the current context

The type arguments for method 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

I admit I don't know anything about LINQ so any ideas how to make this 2nd solution work, or a better one?

like image 766
brokencoding Avatar asked Sep 12 '25 22:09

brokencoding


1 Answers

Don't remove them explicitly, reassign the dictionary using only the values that are above that lower bound instead:

dic = dic.Where(kvp => kvp.Value > lowerBound).ToDictionary();

That way you can completely eliminate that foreach loop you've got in there.

Also, the reason you're getting that error is that you're not using the proper lambda syntax. In fact, you're not using a lambda at all. The expression:

kvp => kvp.Value > lowerBound

Is shorthand for a method that takes one parameter, called "kvp", and returns the evaluation of the expression:

kvp.Value > lowerBound

The => is called the "lambda operator", and it separates the parameter intake from the returned output. Every LINQ method that takes a Func is asking for a delegate of some sort, typically expressed as a lambda expression for brevity. When you give it the lambda expression, the compiler will stop complaining.

Read this for more information on lambda expressions.

like image 134
David Morton Avatar answered Sep 14 '25 13:09

David Morton