Get corresponding key from a given value in Dictionary in C#
This post will discuss how to get the corresponding key from the specified value in the Dictionary in C#.
1. Using Enumerable.FirstOrDefault() method
We can use LINQ’s FirstOrDefault() method to return the first KeyValuePair<K,V> of the dictionary whose value matches with the specified value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static K FindFirstKeyByValue<K, V>(this Dictionary<K, V> dict, V val) { return dict.FirstOrDefault(entry => EqualityComparer<V>.Default.Equals(entry.Value, val)).Key; } } public class Example { public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" }, { "key3", "value3" } }; string value = "value3"; string key = dict.FindFirstKeyByValue(value); if (key != null) { Console.WriteLine("Key \"{0}\" exists for value \"{1}\"", key, value); } else { Console.WriteLine("Key not found for given value"); } } } /* Output: Key "key3" exists for value "value3" */ |
2. Using foreach loop
We can also iterate over the dictionary using a foreach loop and match Value of each KeyValuePair<K,V> pair with the given value. If any match is found, we return the corresponding Key. The following code example demonstrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using System; using System.Collections.Generic; public static class Extensions { public static K FindFirstKeyByValue<K, V>(this Dictionary<K, V> dict, V value) { foreach (KeyValuePair<K, V> pair in dict) { if (EqualityComparer<V>.Default.Equals(pair.Value, value)) { return pair.Key; } } return default; } } public class Example { public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" }, { "key3", "value3" } }; string value = "value3"; string key = dict.FindFirstKeyByValue(value); if (key != null) { Console.WriteLine("Key \"{0}\" exists for value \"{1}\"", key, value); } else { Console.WriteLine("Key not found for given value"); } } } /* Output: Key "key3" exists for value "value3" */ |
3. Inverse Dictionary
If all values in the dictionary are unique, we can create a reverse dictionary where values become keys and keys becomes values.
The following code example shows how to implement this. If any of the values in the dictionary are repeated, the code throws an ArgumentException.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static K FindKeyByValue<K, V>(this Dictionary<K, V> dict, V value) { Dictionary<V, K> revDict = dict.ToDictionary(pair => pair.Value, pair => pair.Key); return revDict[value]; } } public class Example { public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" }, { "key3", "value3" } }; string value = "value3"; string key = dict.FindKeyByValue(value); if (key != null) { Console.WriteLine("Key \"{0}\" exists for value \"{1}\"", key, value); } else { Console.WriteLine("Key not found for given value"); } } } /* Output: Key "key3" exists for value "value3" */ |
That’s all about getting the corresponding key from a given value in Dictionary in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)