C#-এ SortedSet<T> ক্লাস একটি সর্টেড সেট ইমপ্লিমেন্টেশন যা স্বয়ংক্রিয়ভাবে উপাদানগুলো সর্টেড অর্ডারে রাখে। Add(T item) মেথড ব্যবহার করে উপাদান যোগ করা যায়। ডুপ্লিকেট মান যোগ করলে সেটি অগ্রাহ্য করা হয় (ইগনোর হয়)।
উদাহরণ ১: ইন্টিজার SortedSet-এ উপাদান যোগ করা
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedSet<int> set1 = new SortedSet<int>();
set1.Add(100);
set1.Add(200);
set1.Add(300);
set1.Add(400);
Console.WriteLine("SortedSet-এর উপাদান...");
foreach (int res in set1) {
Console.WriteLine(res);
}
Console.WriteLine("SortedSet-এ 500 আছে কি? = " + set1.Contains(500));
}
}
আউটপুট
SortedSet-এর উপাদান... 100 200 300 400 SortedSet-এ 500 আছে কি? = False
উদাহরণ ২: স্ট্রিং SortedSet এবং সুপারসেট চেক
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedSet<string> set1 = new SortedSet<string>();
set1.Add("CD");
set1.Add("CD"); // ডুপ্লিকেট - একবারই থাকবে
set1.Add("CD");
set1.Add("CD");
Console.WriteLine("SortedSet1-এর উপাদান...");
foreach (string res in set1) {
Console.WriteLine(res);
}
SortedSet<string> set2 = new SortedSet<string>();
set2.Add("BC");
set2.Add("CD");
set2.Add("DE");
set2.Add("EF");
set2.Add("AB");
set2.Add("HI");
set2.Add("JK");
Console.WriteLine("SortedSet2-এর উপাদান...");
foreach (string res in set2) {
Console.WriteLine(res);
}
Console.WriteLine("SortedSet2 কি SortedSet1-এর সুপারসেট? = " + set2.IsSupersetOf(set1));
}
}
আউটপুট
SortedSet1-এর উপাদান... CD SortedSet2-এর উপাদান... AB BC CD DE EF HI JK SortedSet2 কি SortedSet1-এর সুপারসেট? = True
ব্যাখ্যা: SortedSet<T> একটি বাইনারি সার্চ ট্রি (লাল-কালো ট্রি) বেসড ইমপ্লিমেন্টেশন যা উপাদানগুলো অর্ডার 유지 করে। ইনসারশন, ডিলিশন, লুকআপ O(log n) সময় নেয়। ডুপ্লিকেট অনুমতি নেই। কাস্টম কম্পারার দিয়ে সর্ট অর্ডার কাস্টোমাইজ করা যায়। সেট অপারেশন (Union, Intersect, Except, IsSubsetOf, IsSupersetOf) বিল্ট-ইন সাপোর্টেড।