দুটি সংগ্রহ থেকে অনন্য লেমেন্টের মিলন পেতে C# এ UnionWith পদ্ধতি ব্যবহার করুন।
ধরা যাক নিম্নলিখিতগুলি আমাদের অভিধান -
Dictionary < string, int > dict1 = new Dictionary < string, int > (); dict1.Add("pencil", 1); dict1.Add("pen", 2); Dictionary < string, int > dict2 = new Dictionary < string, int > (); dict2.Add("pen", 3);
এখন, ইউনিয়ন −
পেতে HashSet এবং UnionWith ব্যবহার করুনHashSet < string > hSet = new HashSet < string > (dict1.Keys); hSet.UnionWith(dict2.Keys);
নিম্নলিখিত সম্পূর্ণ কোড -
উদাহরণ
using System; using System.Collections.Generic; public class Program { public static void Main() { Dictionary < string, int > dict1 = new Dictionary < string, int > (); dict1.Add("pencil", 1); dict1.Add("pen", 2); Dictionary < string, int > dict2 = new Dictionary < string, int > (); dict2.Add("pen", 3); HashSet < string > hSet = new HashSet <string > (dict1.Keys); hSet.UnionWith(dict2.Keys); Console.WriteLine("Merged Dictionary..."); foreach(string val in hSet) { Console.WriteLine(val); } } }
আউটপুট
Merged Dictionary... pencil pen