Check if value already exists in a Dictionary in C#
This post will discuss how to check whether a specific value already exists in a Dictionary<TKey,TValue> in C#.
1. Using Dictionary<TKey,TValue>.ContainsValue() method
The Dictionary<TKey,TValue> class contains the ContainsValue() method, which checks if it contains a specific value or not. It returns a boolean value to indicate the value is found or not.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; using System.Collections.Generic; public class Example { public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("White", "#FFFFFF"); dict.Add("Grey", "#808080"); dict.Add("Silver", "#C0C0C0"); dict.Add("Black", "#000000"); boolean valueExists = dict.ContainsValue("#C0C0C0"); Console.WriteLine(valueExists); // True } } |
Alternatively, you can use the ContainsKey() method to check if a Dictionary<TKey,TValue> contains a specific key.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; using System.Collections.Generic; public class Example { public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("White", "#FFFFFF"); dict.Add("Grey", "#808080"); dict.Add("Silver", "#C0C0C0"); dict.Add("Black", "#000000"); boolean keyExists = dict.ContainsValue("Silver"); Console.WriteLine(keyExists); // True } } |
2. Using Enumerable.Any() method
The Any() method returns true if any element of the sequence satisfies the provided condition. To check whether a value exists in a dictionary, you can use it like below. Note that you need to include the System.Linq namespace.
|
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 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static bool hasValue<K,V>(this IDictionary<K,V> dict, V value) { return dict.Any(kvp => kvp.Value.Equals(value)); } } public class Example { public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("White", "#FFFFFF"); dict.Add("Grey", "#808080"); dict.Add("Silver", "#C0C0C0"); dict.Add("Black", "#000000"); boolean valueExists = dict.hasValue("#C0C0C0"); Console.WriteLine(valueExists); // True } } |
3. Using Custom Routine
The more traditional approach to finding a value in a dictionary involves iterating through the dictionary and checking each key-value pair for the matching value. In C#, this translates to:
|
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 |
using System; using System.Collections.Generic; public static class Extensions { public static bool hasValue<K,V>(this IDictionary<K,V> dict, V value) { foreach (KeyValuePair<K,V> kvp in dict) { if (kvp.Value.Equals(value)) { return true; } } return false; } } public class Example { public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("White", "#FFFFFF"); dict.Add("Grey", "#808080"); dict.Add("Silver", "#C0C0C0"); dict.Add("Black", "#000000"); boolean valueExists = dict.hasValue("#C0C0C0"); Console.WriteLine(valueExists); // True } } |
The above solution iterates over the dictionary mappings using the foreach loop. You can directly iterate over all the values contained in the dictionary using the Dictionary<TKey,TValue>.Values property.
|
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 |
using System; using System.Collections.Generic; public static class Extensions { public static bool hasValue<K,V>(this IDictionary<K,V> dict, V value) { foreach(V v in dict.Values) { if (v.Equals(value)) { return true; } } return false; } } public class Example { public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("White", "#FFFFFF"); dict.Add("Grey", "#808080"); dict.Add("Silver", "#C0C0C0"); dict.Add("Black", "#000000"); boolean valueExists = dict.hasValue("#C0C0C0"); Console.WriteLine(valueExists); // True } } |
That’s all about checking whether a specific value already exists in a Dictionary<TKey,TValue> 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 :)