परिचय
C# में SortedDictionary.Remove() मेथड SortedDictionary<TKey, TValue> से निर्दिष्ट कुंजी वाले तत्व को हटाता है। यदि कुंजी मिलती है और हटा दी जाती है तो true वापस करता है, अन्यथा false।
सिंटैक्स
public bool Remove(TKey key);
key हटाए जाने वाले तत्व की कुंजी है।
उदाहरण 1: इंटीजर कुंजियों के साथ
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(100, "Mobile");
sortedDict.Add(200, "Laptop");
sortedDict.Add(300, "Desktop");
sortedDict.Add(400, "Speakers");
sortedDict.Add(500, "Headphone");
sortedDict.Add(600, "Earphone");
Console.WriteLine("SortedDictionary कुंजी-मान जोड़े...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("\nकुंजी 400 हटाई गई? = " + sortedDict.Remove(400));
Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़े...अपडेटेड");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
SortedDictionary.KeyCollection keyColl = sortedDict.Keys;
Console.WriteLine("\nकुंजियों की सूची...");
foreach (int i in keyColl)
Console.WriteLine(i);
}
}
आउटपुट
SortedDictionary कुंजी-मान जोड़े... Key = 100, Value = Mobile Key = 200, Value = Laptop Key = 300, Value = Desktop Key = 400, Value = Speakers Key = 500, Value = Headphone Key = 600, Value = Earphone कुंजी 400 हटाई गई? = True SortedDictionary कुंजी-मान जोड़े...अपडेटेड Key = 100, Value = Mobile Key = 200, Value = Laptop Key = 300, Value = Desktop Key = 500, Value = Headphone Key = 600, Value = Earphone कुंजियों की सूची... 100 200 300 500 600
उदाहरण 2: स्ट्रिंग मानों के साथ
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(1, "Ultrabook");
sortedDict.Add(2, "Alienware");
sortedDict.Add(3, "Notebook");
sortedDict.Add(4, "Connector");
sortedDict.Add(5, "Flash Drive");
sortedDict.Add(6, "SSD");
sortedDict.Add(7, "HDD");
sortedDict.Add(8, "Earphone");
Console.WriteLine("SortedDictionary कुंजी-मान जोड़े...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("\nकुंजी 7 हटाई गई? = " + sortedDict.Remove(7));
Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़े...अपडेटेड");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
SortedDictionary.KeyCollection keyColl = sortedDict.Keys;
Console.WriteLine("\nकुंजियों की सूची...");
foreach (int i in keyColl)
Console.WriteLine(i);
}
}
आउटपुट
SortedDictionary कुंजी-मान जोड़े... Key = 1, Value = Ultrabook Key = 2, Value = Alienware Key = 3, Value = Notebook Key = 4, Value = Connector Key = 5, Value = Flash Drive Key = 6, Value = SSD Key = 7, Value = HDD Key = 8, Value = Earphone कुंजी 7 हटाई गई? = True SortedDictionary कुंजी-मान जोड़े...अपडेटेड Key = 1, Value = Ultrabook Key = 2, Value = Alienware Key = 3, Value = Notebook Key = 4, Value = Connector Key = 5, Value = Flash Drive Key = 6, Value = SSD Key = 8, Value = Earphone कुंजियों की सूची... 1 2 3 4 5 6 8
मुख्य बिंदु
- यदि कुंजी मौजूद नहीं है तो
falseवापस करता है (कोई अपवाद नहीं)। - हटाने के बाद शेष तत्व सॉर्टेड क्रम में रहते हैं।
- समय जटिलता: O(log n) - बाइनरी सर्च ट्री संरचना के कारण।
Clear()सभी तत्वों को एक साथ हटाता है।