Merge dictionaries in C#
This post will discuss how to merge two or more dictionaries in C#. The solution should append key-value pairs present in all dictionaries into a new dictionary.
1. Using LINQ
We can use LINQ to merge any number of dictionaries in C#. The following code example shows how to implement this.
|
1 2 3 4 5 |
public static Dictionary<K, V> Merge<K, V>(IEnumerable<Dictionary<K, V>> dictionaries) { return dictionaries.SelectMany(x => x) .ToDictionary(x => x.Key, y => y.Value); } |
If any of the keys gets repeated in any dictionary, the above code throws an ArgumentException. To handle this, the idea is to group elements based on the key before converting it to a dictionary.
|
1 2 3 4 5 6 |
public static Dictionary<K, V> Merge<K, V>(IEnumerable<Dictionary<K, V>> dictionaries) { return dictionaries.SelectMany(x => x) .GroupBy(d => d.Key) .ToDictionary(x => x.Key, y => y.First().Value); } |
We can also use the ToLookup() method to get multiple values per key, as shown below:
|
1 2 3 4 5 6 |
public static Dictionary<K, V> Merge<K, V>(IEnumerable<Dictionary<K, V>> dictionaries) { return dictionaries.SelectMany(x => x) .ToLookup(pair => pair.Key, pair => pair.Value) .ToDictionary(g => g.Key, g => g.First()); } |
2. Using Dictionary.Add() method
Here, the idea is to create a new dictionary, iterate over each dictionary, and manually add each pair to the new dictionary. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 |
public static Dictionary<K, V> Merge<K, V>(IEnumerable<Dictionary<K, V>> dictionaries) { Dictionary<K, V> result = new Dictionary<K, V>(); foreach (Dictionary<K, V> dict in dictionaries) { dict.ToList().ForEach(pair => result.Add(pair.Key, pair.Value)); } return result; } |
The above code throws an ArgumentException, when attempting to add a duplicate key. To update the value of a key already present in the Dictionary, we can use dict[key] = value syntax instead of the Add() method. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 |
public static Dictionary<K, V> Merge<K, V>(IEnumerable<Dictionary<K, V>> dictionaries) { Dictionary<K, V> result = new Dictionary<K, V>(); foreach (Dictionary<K, V> dict in dictionaries) { dict.ToList().ForEach(pair => result[pair.Key] = pair.Value); } return result; } |
3. Using Union() method
Alternatively, we can use the Union() method to facilitate the merge of dictionaries into one another.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public static Dictionary<K, V> Merge<K, V>(IEnumerable<Dictionary<K, V>> dictionaries) { Dictionary<K, V> result = new Dictionary<K, V>(); foreach (Dictionary<K, V> dict in dictionaries) { result = result.Union(dict) .ToDictionary(pair => pair.Key, pair => pair.Value); } return result; } |
The above code throws an ArgumentException if it reencounters the same key. To handle the duplicate keys, the idea remains the same – group elements based on the key before converting to a dictionary.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static Dictionary<K, V> Merge<K, V>(IEnumerable<Dictionary<K, V>> dictionaries) { Dictionary<K, V> result = new Dictionary<K, V>(); foreach (Dictionary<K, V> dict in dictionaries) { result = result.Union(dict) .GroupBy(g => g.Key) .ToDictionary(pair => pair.Key, pair => pair.First().Value); } return result; } |
That’s all about merging dictionaries in C#.
Also See:
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 :)