Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में Dictionary में विशिष्ट वैल्यू है या नहीं जांचें

Dictionary<TKey, TValue>.ContainsValue() मेथड का उपयोग करके जांचें कि डिक्शनरी में विशिष्ट वैल्यू मौजूद है या नहीं।

सिंटैक्स

public bool ContainsValue(TValue value);

उदाहरण 1: वैल्यू मिलने पर

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict.Add("One", "John");
        dict.Add("Two", "Tom");
        dict.Add("Three", "Jacob");
        dict.Add("Four", "Kevin");
        dict.Add("Five", "Nathan");
        
        Console.WriteLine("Count of elements = " + dict.Count);
        Console.WriteLine("\nKey/value pairs...");
        foreach (KeyValuePair<string, string> res in dict) {
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }
        
        if (dict.ContainsValue("Kevin")) {
            Console.WriteLine("Value found!");
        } else {
            Console.WriteLine("Value isn't in the dictionary!");
        }
        
        dict.Clear();
        Console.WriteLine("Cleared Key/value pairs...");
        Console.WriteLine("Count of elements now = " + dict.Count);
    }
}

आउटपुट

Count of elements = 5
Key/value pairs...
Key = One, Value = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan
Value found!
Cleared Key/value pairs...
Count of elements now = 0

उदाहरण 2: वैल्यू न मिलने पर

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict.Add("One", "Chris");
        dict.Add("Two", "Steve");
        dict.Add("Three", "Messi");
        dict.Add("Four", "Ryan");
        dict.Add("Five", "Nathan");
        
        Console.WriteLine("Count of elements = " + dict.Count);
        Console.WriteLine("\nKey/value pairs...");
        foreach (KeyValuePair<string, string> res in dict) {
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }
        
        if (dict.ContainsValue("Angelina")) {
            Console.WriteLine("Value found!");
        } else {
            Console.WriteLine("Value isn't in the dictionary!");
        }
        
        dict.Clear();
        Console.WriteLine("Cleared Key/value pairs...");
        Console.WriteLine("Count of elements now = " + dict.Count);
    }
}

आउटपुट

Count of elements = 5
Key/value pairs...
Key = One, Value = Chris
Key = Two, Value = Steve
Key = Three, Value = Messi
Key = Four, Value = Ryan
Key = Five, Value = Nathan
Value isn't in the dictionary!
Cleared Key/value pairs...
Count of elements now = 0

महत्वपूर्ण बिंदु

  • ContainsValue O(n) है - सभी वैल्यूज को लीनियरली खोजता है
  • ContainsKey O(1) है - हैश लुकअप का उपयोग करता है
  • यदि की ज्ञात है, तो ContainsKey या TryGetValue प्राथमिकता दें
  • डुप्लीकेट वैल्यूज अनुमत हैं - पहला मिलान काफी है
  • नल वैल्यू के लिए भी काम करता है

संबंधित मेथड्स

मेथडविवरणकॉम्प्लेक्सिटी
ContainsKey(key)की मौजूद है?O(1)
ContainsValue(value)वैल्यू मौजूद है?O(n)
TryGetValue(key, out value)की से वैल्यू प्राप्त करेंO(1)
this[key]इंडेक्सर (की मौजूद न होने पर एक्सेप्शन)O(1)

बेस्ट प्रैक्टिस: TryGetValue पैटर्न

// खराब: दो लुकअप्स
if (dict.ContainsKey(key)) {
    var value = dict[key];
}

// अच्छा: एक लुकअप
if (dict.TryGetValue(key, out var value)) {
    // value उपयोग करें
}

// केवल वैल्यू चेक के लिए (की अनजान)
if (dict.ContainsValue(targetValue)) { ... }

LINQ विकल्प

// किसी भी वैल्यू के लिए प्रेडिकेट
bool hasLongName = dict.Values.Any(v => v.Length > 10);

// पहली मिलान वाली की ढूंढें
var key = dict.FirstOrDefault(kvp => kvp.Value == "Kevin").Key;
  1. - C# प्रोग्राम: जांचना कि दो अनुक्रम समान हैं या नहीं

    SequenceEqual मेथड का उपयोग संग्रहों की समानता के लिए परीक्षण करने के लिए किया जाता है।अनुक्रम सेट करें:string[] arr1 = { "This", "is", "it" }; string[] arr2 = { "My", "work", "report" }; string[] arr3 = { "This", "is",

  1. - C# में आर्मस्ट्रांग संख्या जांचना

    आर्मस्ट्रांग संख्या (Armstrong Number) जिसे नार्सिसिस्टिक संख्या भी कहते हैं, एक ऐसी संख्या है जो अपने अंकों की घातों के योग के बराबर होती है। n अंकों की संख्या के लिए: प्रत्येक अंक की nवीं घात का योग संख्या के बराबर होता है। उदाहरण: 3-अंकीय संख्याओं के लिए 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 ✓

  1. - Python Pandas - जाँचें कि DateOffset मान सामान्यीकृत है या नहीं

    DateOffset ऑब्जेक्ट का सामान्यीकरण जाँचें Pandas में offset.normalize प्रॉपर्टी का उपयोग करके यह जाँचा जा सकता है कि DateOffset मान सामान्यीकृत है या नहीं। आवश्यक लाइब्रेरी आयात करें from pandas.tseries.offsets import DateOffset import pandas as pd टाइमस्टैम्प ऑब्जेक्ट सेट करें timestamp = pd.Time