
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# program to find Union of two or more Dictionariesn
Firstly, set both the dictionaries −
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);
Now, create HashSet and use UnionsWith() method to find the union between the above two Dictionaries −
HashSet < string > hSet = new HashSet < string > (dict1.Keys); hSet.UnionWith(dict2.Keys);
The following is the complete code −
Example
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..."); foreach(string val in hSet) { Console.WriteLine(val); } } }
Output
Union of Dictionary... water food clothing shelter
- Related Articles
- C# program to find Union of two or more Lists
- Python program to find Union of two or more Lists?
- C++ program to find union and intersection of two unsorted arrays
- Program to find union of two given linked lists in Python
- Python program to merge two Dictionaries
- C# program to merge two Dictionaries
- C# program to find common values from two or more Lists
- Java Program to Calculate union of two sets
- Golang program to calculate union of two slices
- C# program to concat two or more Lists
- Python Program to Concatenate Two Dictionaries Into One
- Python Program to compare elements in two dictionaries
- C++ Program to Find the Shortest Supersequence that Contains Two or more Sequences as Subsequences
- How to find the union of two vectors in R?
- How to Find the Union of Two Arrays in Java?

Advertisements