Return number of matches from c# dictionary - c#

I have a dictionary with non unique values and I want to count the matches of a string versus the values.
Basically I now do dict.ContainsValue(a) to get a bool telling me if the string a exists in dict, but I want to know not only if it exists but how many times it exists (and maybee even get a list of the keys it exists bound to)
Is there a way to do this using dictionary, or should I look for a different collection?
/Rickard Haake

To get the number of instances of the value you could do something like this:
dict.Values.Count(v => v == a);
To find the keys that have this value you could do this:
dict.Where(kv => kv.Value == a).Select(kv => kv.Key);

To get the count use Values.Count:
int count = dict.Values.Count(x => x == "foo");
To get the keys I prefer the query syntax:
var keys = from kvp in dict
where kvp.Value == "foo"
select kvp.Key;
Note that this will require scanning the entire dictionary. For small dictionaries or infrequent lookups this may not be a problem.
If you are making many lookups you may wish to maintain a second dictionary that maps the values to the keys. Whilst this will speed up lookups, it will slow down modifications as both dictionaries will need updating for each change.

what about using LINQ: if a is the value you're looking for, the the code could be
dict.Values.Where(v => v == a).Count();

Related

Can I use LINQ to find out if a given property in a list repeats itself?

I have a list of objects that have a name field on them.
I want to know if there's a way to tell if all the name fields are unique in the list.
I could just do two loops and iterate over the list for each value, but I wanted to know if there's a cleaner way to do this using LINQ?
I've found a few examples where they compare each item of the list to a hard coded value but in my case I want to compare the name field on each object between each other and obtain a boolean value.
A common "trick" to check for uniqueness is to compare the length of a list with duplicates removed with the length of the original list:
bool allNamesAreUnique = myList.Select(x => x.Name).Distinct().Count() == myList.Count();
Select(x => x.Name) transforms your list into a list of just the names, and
Distict() removes the duplicates.
The performance should be close to O(n), which is better than the O(n²) nested-loop solution.
Another option is to group your list by the name and check the size of those groups. This has the additional advantage of telling you which values are not unique:
var duplicates = myList.GroupBy(x => x.Name).Where(g => g.Count() > 1);
bool hasDuplicates = duplicates.Any(); // or
List<string> duplicateNames = duplicates.Select(g => g.Key).ToList();
While you can use LINQ to group or create a distinct list, and then compare item-wise with the original list, that incurs a bit of overhead you might not want, especially for a very large list. A more efficient solution would store the keys in a HashSet, which has better lookup capability, and check for duplicates in a single loop. This solution still uses a little bit of LINQ so it satisfies your requirements.
static public class ExtensionMethods
{
static public bool HasDuplicates<TItem,TKey>(this IEnumerable<TItem> source, Func<TItem,TKey> func)
{
var found = new HashSet<TKey>();
foreach (var key in source.Select(func))
{
if (found.Contains(key)) return true;
found.Add(key);
}
return false;
}
}
If you are looking for duplicates in a field named Name, use it like this:
var hasDuplicates = list.HasDuplicates( item => item.Name );
If you want case-insensitivity:
var hasDuplicates = list.HasDuplicates( item => item.Name.ToUpper() );

Search For keys Matching values in a dictionary of list in C#

I have a dictionary of lists.
var dicAclWithCommonDsEffectivity = new Dictionary<string, List<int>>();
var list1 = new List<int>(){1,2,3};
var list2 = new List<int>(){2,4,6};
var list3 = new List<int>(){3,7,6};
var list4 = new List<int>(){8,7,6};
dicAclWithCommonDsEffectivity.Add("ab",list1);
dicAclWithCommonDsEffectivity.Add("bc",list2);
dicAclWithCommonDsEffectivity.Add("cd",list3);
dicAclWithCommonDsEffectivity.Add("de",list4);
I want to get the keys in dictionary for which atleast one matching value with the current key list.
for key "ab"(first list). I should get: "ab","bc" and "cd".Since these lists contain one of the matching element in {1,2,3}
Is there a way without looping through each item in the list of dictionary value.
Is there a way without looping through each item in the list of dictionary value.
Something has to loop - dictionaries are only designed to look up by key, and you're not doing that other than for the first check.
You can do this fairly easily though:
private IEnumerable<string> GetMatchingKeys(
Dictionary<string, List<int>> dictionary, string key)
{
// TODO: Use TryGetValue if key might not be in dictionary
HashSet<int> elements = new HashSet<int>(dictionary[key]);
return dictionary.Where(pair => pair.Value.Any(x => elements.Contains(x)))
.Select(pair => pair.Key);
}
This uses the fact that Dictionary implements IEnumerable<KeyValuePair<TKey, TValue>> - so the Where clause checks a particular entry by spotting if any of the elements of its value matches any of the elements of the original value. The Select clause then projects the pair to just the key.
If you need to do this a lot and you're concerned about efficiency, another alternative would be to build a second dictionary from int to List<string> - basically a reverse mapping. You'd need to maintain that, but then you could easily fetch all the "original keys" mapping to each of the values corresponding to the given key, and just use Distinct to avoid duplicates.
It's always better to check for existence of the searchKey in the Dictionary before accessing them. Then you can get the associated list in the dictionary for that particular key.
You can also try like this:
string searchKey = "ab";
if (dicAclWithCommonDsEffectivity.ContainsKey(searchKey))
{
var ListToSearch = dicAclWithCommonDsEffectivity[searchKey];
var resultKeys = dicAclWithCommonDsEffectivity.Where(x =>
x.Value.Any(y => ListToSearch.Contains(y)))
.Select(x => x.Key)
.ToList();
}
else
{
// Specified key was not found
}
If what you mean is to visibly (not logically) remove the looping, you could use LINQ with proper Where filter to do that and Select the keys from the Dictionary which have any value element(s) intersect(s) with the selected List (List in the Dictionary with key == "ab") like this:
string key = "ab";
List<int> selectedList = dicAclWithCommonDsEffectivity[key];
var results = dicAclWithCommonDsEffectivity
.Where(x => x.Value.Any(y => selectedList.Contains(y)))
.Select(x => x.Key);
If you want to logically remove the looping too, please consider Mr. Skeet's answer.

linq one liner to remove elements from IDictionary whose keys are not in another Dictionary

I have
IDictionary<string,object> d1;
Dictionary<string, object> d2;
I need to remove from d1 all entries that are not in d2.
I know I can do this with a for loop etc but that's so last century; I want to do it right.
I got to
d1.Where(x => {return d2.ContainsKey(x.key);});
but dont know what to do next
LINQ isn't designed to modify existing elements - but you could always create a new dictionary. For example:
d1 = d1.Where(x => d2.ContainsKey(x.Key))
.ToDictionary(x => x.Key, x => x.Value);
Or:
d1 = d1.Keys.Intersect(d2.Keys)
.ToDictionary(key => x.Key, key => d1[key]);
As others have said, if you're more keen on doing a Remove operation, I'd just loop. For example:
foreach (var key in d1.Keys.Except(d2.Keys).ToList())
{
d1.Remove(key);
}
(I'm not sure why you used a statement lambda in your sample code, by the way.)
LINQ is for querying. The information you're querying is the keys from d1 that are not in d2. Other than missing a NOT (unless you didn't mean to say "not" in your requirements). you already have that. When it comes to actually doing something, that's best done with a foreach loop, not LINQ:
foreach(var pair in d1.Where(x=> !d2.ContainsKey(x.Key)).ToList())
{
d1.Remove(pair.Key)
}
Note that the ToList is needed to ensure that you are not modifying a collection while iterating it.

Understanding lambda expressions in C#

I'm new to lambdas and they seemed fairly straight-forward until I tried to do something more complex.
I have this dictionary.
Dictionary<int, int> dict = new Dictionary<int,int>();
of which I want to obtain the key of the key-val pair with the largest value. What I tried is:
dict.Keys.Max(g => dict[g])
The reasoning being that out of the list of Keys, pick that one for which dict[key] is largest. However, this picks the largest value itself, rather than its corresponding key.
dict.Keys.OrderByDescending(g => dict[g]).First() will accomplish what you want, but may be inefficient for large dictionaries. MaxBy in John Skeet's MoreLinq will do exactly what you want efficiently.
var maxValue = dict.Max((maxPair) => maxPair.Value);
var maxPairs = dict.Where((pair) => pair.Value == maxValue);
This will give you a list of all of the pairs that have the maximum value.
If you just want the keys, you can do this afterwards:
var maxKeys = maxPairs.Select((pair) => pair.Key);
I decided to add an answer based on my thoughts on McKay's. This will perform very fast given the standard LINQ methods, provides just the key:
var maxValue = dict.Max(p => p.Value);
var keys = dict.Where(p => p.Value == maxValue).Select(p => p.Key);
Now, if the OP knows that there is always just one key (no duplicate values) then an improvement (very small) would be to use First with this as due to lazy evaluation only the elements up to the one with the maximum value would be evaluated after all were evaluated to first find the maximum value:
var key = dict.Where(p => p.Value == maxValue).First().Key;
dict.OrderBy(v => v.Value).Last().Key;
should do it. Basically you are ordering the KeyValuePair by value and picking the last one which will be the maximum. And within the last one you are only interested in the Key.

Need to look up key using string in a Dictionary<TKey, TValue> Class

I need to use a list pulled from sql, list is built from
Dictionary<Int32, String> measurementTypes = this.GetIndicatorTypes(MeasurementTypeFilter.All);
is ther a way to retrive the key using the string.
Something like
TypeID = measurementTypes.contains("GEN");
Well, it'll be slow (i.e. O(n)), but you can do:
var keys = measurementTypes.Where(pair => pair.Value == "GEN")
.Select(pair => pair.Key);
That will give you a sequence of pairs which have the given value. There could be 0, 1 or many matches. From there you can pick the first matching key etc - whatever you need. Using First or Single would be appropriate if you think there will be at least one or exactly one; FirstOrDefault would return 0 if there were no matches, which may not be appropriate for you if 0 could also be a valid key.
TypeID = measurementTypes.Values.Where(v => v.Equals("GEN")).FirstOrDefault();
You can use LINQ to help you out there.
TypeID = measurementTypes.Where(kvp => kvp.Value == "GEN")
.Select(kvp => kvp.Key)
.FirstOrDefault()

Categories