परिचय
C# में SortedDictionary.ContainsValue() मेथड यह निर्धारित करता है कि SortedDictionary<TKey, TValue> में निर्दिष्ट मान वाला कोई तत्व है या नहीं।
सिंटैक्स
public bool ContainsValue(TValue value);
value SortedDictionary<TKey, TValue> में खोजा जाने वाला मान है।
उदाहरण 1: मौजूद मान खोजना
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(100, "Inspiron");
sortedDict.Add(200, "Alienware");
sortedDict.Add(300, "Projectors");
sortedDict.Add(400, "XPS");
Console.WriteLine("SortedDictionary कुंजी-मान जोड़े...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या = " + sortedDict.Count);
sortedDict.Add(800, "Notebook");
sortedDict.Add(10000, "Bluetooth Speaker");
Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़े...अपडेटेड");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);
Console.WriteLine("क्या मान SortedDictionary में है? = " + sortedDict.ContainsValue("Notebook"));
sortedDict.Clear();
Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);
}
}
आउटपुट
SortedDictionary कुंजी-मान जोड़े... Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS SortedDictionary कुंजी-मान जोड़ों की संख्या = 4 SortedDictionary कुंजी-मान जोड़े...अपडेटेड Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS Key = 800, Value = Notebook Key = 10000, Value = Bluetooth Speaker SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 6 क्या मान SortedDictionary में है? = True SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 0
उदाहरण 2: अनुपस्थित मान खोजना
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(1, "One");
sortedDict.Add(2, "Two");
sortedDict.Add(3, "Three");
sortedDict.Add(4, "Four");
sortedDict.Add(5, "Five");
sortedDict.Add(6, "Six");
Console.WriteLine("SortedDictionary कुंजी-मान जोड़े...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या = " + sortedDict.Count);
sortedDict.Add(7, "Seven");
sortedDict.Add(8, "Eight");
Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़े...अपडेटेड");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);
Console.WriteLine("क्या मान SortedDictionary में है? = " + sortedDict.ContainsValue("Eleven"));
sortedDict.Clear();
Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);
}
}
आउटपुट
SortedDictionary कुंजी-मान जोड़े... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six SortedDictionary कुंजी-मान जोड़ों की संख्या = 6 SortedDictionary कुंजी-मान जोड़े...अपडेटेड Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Key = 7, Value = Seven Key = 8, Value = Eight SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 8 क्या मान SortedDictionary में है? = False SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 0
मुख्य बिंदु
ContainsValue()रैखिक खोज (linear search) करता है - O(n) समय जटिलता।ContainsKey()O(log n) है (बाइनरी सर्च ट्री) - कुंजी द्वारा खोज बहुत तेज है।- मान डुप्लीकेट हो सकते हैं - पहला मिलान पर्याप्त है।
nullमान के लिए भी काम करता है यदि TValue रेफरेंस टाइप या नलेएबल है।- केस-सेंसिटिव स्ट्रिंग तुलना के लिए डिफ़ॉल्ट तुलनाकर्ता का उपयोग करता है।