The solution uses HashSet's UnionWith instance method. This combines the Hash Set with another collection of the same value type.
using System;
using System.Collections.Generic;
// Part 1: create dictionaries.
Dictionary<string, int> d1 = new Dictionary<string, int>();
d1.Add(
"cat", 1);
d1.Add(
"dog", 2);
Dictionary<string, int> d2 = new Dictionary<string, int>();
d2.Add(
"cat", 3);
d2.Add(
"man", 4);
Dictionary<string, int> d3 = new Dictionary<string, int>();
d3.Add(
"sheep", 5);
d3.Add(
"fish", 6);
d3.Add(
"cat", 7);
// Part 2: get union of all keys.
HashSet<string> h1 = new HashSet<string>(d1.Keys);
h1.UnionWith(d2.Keys);
h1.UnionWith(d3.Keys);
// Part 3: display all the keys.
// ... You can look up the values in the loop body.
foreach (string k in h1)
{
Console.WriteLine(k);
}
cat
dog
man
sheep
fish