C# program to find Union of two or more Dictionaries

The union of two or more dictionaries combines all unique keys from the dictionaries. In C#, you can find the union using HashSet<T> and the UnionWith() method to merge dictionary keys, or use LINQ methods to combine both keys and values.

Syntax

Following is the syntax for finding union of dictionary keys using HashSet −

HashSet<TKey> unionKeys = new HashSet<TKey>(dict1.Keys);
unionKeys.UnionWith(dict2.Keys);

Following is the syntax for merging dictionary values using LINQ −

var unionDict = dict1.Union(dict2).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

Using HashSet for Key Union

The most straightforward approach is to extract all keys from the dictionaries and combine them using HashSet

using System;
using System.Collections.Generic;

public class Program {
   public static void Main() {
      Dictionary<string, int> dict1 = new Dictionary<string, int>();
      dict1.Add("water", 1);
      dict1.Add("food", 2);

      Dictionary<string, int> dict2 = new Dictionary<string, int>();
      dict2.Add("clothing", 3);
      dict2.Add("shelter", 4);

      HashSet<string> hSet = new HashSet<string>(dict1.Keys);
      hSet.UnionWith(dict2.Keys);

      Console.WriteLine("Union of Dictionary Keys...");
      foreach(string val in hSet) {
         Console.WriteLine(val);
      }
   }
}

The output of the above code is −

Union of Dictionary Keys...
water
food
clothing
shelter

Using LINQ for Complete Dictionary Union

To merge dictionaries with both keys and values, use LINQ's Union() method −

using System;
using System.Collections.Generic;
using System.Linq;

public class Program {
   public static void Main() {
      Dictionary<string, int> dict1 = new Dictionary<string, int> {
         {"apple", 10},
         {"banana", 20}
      };

      Dictionary<string, int> dict2 = new Dictionary<string, int> {
         {"orange", 30},
         {"grape", 40}
      };

      var unionDict = dict1.Union(dict2).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

      Console.WriteLine("Union of Dictionaries:");
      foreach(var item in unionDict) {
         Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");
      }
   }
}

The output of the above code is −

Union of Dictionaries:
Key: apple, Value: 10
Key: banana, Value: 20
Key: orange, Value: 30
Key: grape, Value: 40

Union of Multiple Dictionaries

You can also find the union of more than two dictionaries by chaining multiple UnionWith() calls −

using System;
using System.Collections.Generic;

public class Program {
   public static void Main() {
      Dictionary<string, int> dict1 = new Dictionary<string, int> {
         {"red", 1}, {"blue", 2}
      };
      Dictionary<string, int> dict2 = new Dictionary<string, int> {
         {"green", 3}, {"yellow", 4}
      };
      Dictionary<string, int> dict3 = new Dictionary<string, int> {
         {"purple", 5}, {"orange", 6}
      };

      HashSet<string> unionKeys = new HashSet<string>(dict1.Keys);
      unionKeys.UnionWith(dict2.Keys);
      unionKeys.UnionWith(dict3.Keys);

      Console.WriteLine("Union of Three Dictionaries:");
      foreach(string key in unionKeys) {
         Console.WriteLine(key);
      }
      Console.WriteLine($"Total unique keys: {unionKeys.Count}");
   }
}

The output of the above code is −

Union of Three Dictionaries:
red
blue
green
yellow
purple
orange
Total unique keys: 6

Conclusion

Finding the union of dictionaries in C# can be accomplished using HashSet.UnionWith() for keys only, or LINQ's Union() method for complete dictionary merging. The HashSet approach is efficient for key-only operations, while LINQ provides more flexibility for combining both keys and values.

Updated on: 2026-03-17T07:04:35+05:30

810 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements