C#-এ SortedList ক্লাসের Remove(object key) মেথড ব্যবহার করে নির্দিষ্ট কী-এর সাথে যুক্ত উপাদান (কী-মান জোড়া) মুছে ফেলা যায়। SortedList কী অনুযায়ী সর্টেড থাকে, তাই কী-বেসড রিমুভ O(log n) সময়ে হয় বাইনারি সার্চ ব্যবহার করে।
উদাহরণ ১: "H" কী মুছে ফেলা
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList sortedList = new SortedList();
sortedList.Add("A", "1");
sortedList.Add("B", "2");
sortedList.Add("C", "3");
sortedList.Add("D", "4");
sortedList.Add("E", "5");
sortedList.Add("F", "6");
sortedList.Add("G", "7");
sortedList.Add("H", "8");
sortedList.Add("I", "9");
sortedList.Add("J", "10");
Console.WriteLine("SortedList উপাদান...");
foreach (DictionaryEntry d in sortedList) {
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("SortedList কী-মান জোড়ার সংখ্যা = " + sortedList.Count);
// "H" কী মুছে ফেলা
sortedList.Remove("H");
Console.WriteLine("\nEnumerator দিয়ে traverser করা হচ্ছে...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("SortedList কী-মান জোড়ার সংখ্যা (আপডেটেড) = " + sortedList.Count);
}
}
আউটপুট
SortedList উপাদান... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = H, Value = 8 Key = I, Value = 9 Key = J, Value = 10 SortedList কী-মান জোড়ার সংখ্যা = 10 Enumerator দিয়ে traverser করা হচ্ছে... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = I, Value = 9 Key = J, Value = 10 SortedList কী-মান জোড়ার সংখ্যা (আপডেটেড) = 9
উদাহরণ ২: "Three" কী মুছে ফেলা
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList sortedList = new SortedList();
sortedList.Add("One", "Mouse");
sortedList.Add("Two", "Keyboard");
sortedList.Add("Three", "Headphone");
sortedList.Add("Four", "Speakers");
sortedList.Add("Five", "RAM");
Console.WriteLine("SortedList উপাদান...");
foreach (DictionaryEntry d in sortedList) {
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("SortedList কী-মান জোড়ার সংখ্যা = " + sortedList.Count);
sortedList.Remove("Three");
Console.WriteLine("\nEnumerator দিয়ে traverser করা হচ্ছে...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("SortedList কী-মান জোড়ার সংখ্যা (আপডেটেড) = " + sortedList.Count);
}
}
আউটপুট
SortedList উপাদান... Key = Five, Value = RAM Key = Four, Value = Speakers Key = One, Value = Mouse Key = Three, Value = Headphone Key = Two, Value = Keyboard SortedList কী-মান জোড়ার সংখ্যা = 5 Enumerator দিয়ে traverser করা হচ্ছে... Key = Five, Value = RAM Key = Four, Value = Speakers Key = One, Value = Mouse Key = Two, Value = Keyboard SortedList কী-মান জোড়ার সংখ্যা (আপডেটেড) = 4
ব্যাখ্যা: Remove(key) মেথড কী খুঁজে পেলে সংশ্লিষ্ট এন্ট্রিটি মুছে ফেলে। কী না পেলে কিছুই করে না (এক্সেপশন থ্রো করে না)। SortedList অ্যারে-বেসড ইমপ্লিমেন্টেশন, তাই রিমুভের পর বাকি উপাদানগুলো শিফট করতে হয় (O(n) ওয়ার্স্ট কেস)। কী-বেসড লুকআপ O(log n)। নতুন প্রজেক্টে জেনেরিক SortedList<TKey, TValue> বা SortedDictionary<TKey, TValue> (ট্রি-বেসড, দ্রুত ইনসারশন/ডিলিশন) ব্যবহার করা উচিত।