Menu
Coddy logo textTech

Checking if an Element Exists

Coddy'nin C# Journey'sinin Mantık & Akış bölümünün bir parçası — ders 59 / 66.

Contains(element) yöntemi, HashSet içinde belirli bir öğenin mevcut olup olmadığını kontrol eder. Öğe bulunursa true, aksi takdirde false döndürür.

Boş bir HashSet oluşturun:

HashSet<string> fruits = new HashSet<string>();

HashSet'e bazı öğeler ekleyin:

fruits.Add("Apple");
fruits.Add("Banana");

Bir öğenin var olup olmadığını kontrol edin:

bool hasApple = fruits.Contains("Apple");  // Returns true
bool hasOrange = fruits.Contains("Orange"); // Returns false

Sonucu koşullu ifadelerde kullanabilirsiniz:

if (fruits.Contains("Apple"))
{
    Console.WriteLine("Apple is in the set");
}
challenge icon

Görev

Kolay

ElementExists adında bir metot oluşturun ki iki argüman alsın:

  • String türünden bir HashSet (set)
  • Kontrol edilecek bir string (element)

Metot, elemanın sette mevcut olup olmadığını kontrol etmeli ve bir string mesaj döndürmeli:

  • Eğer eleman mevcutsa, "The element 'X' exists in the set" döndürün
  • Eğer eleman mevcut değilse, "The element 'X' does not exist in the set" döndürün

'X', kontrol edilen gerçek eleman değeridir.

Kendin dene

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Program
{
    public static string ElementExists(HashSet<string> set, string element)
    {
        // Kodunuzu buraya yazın
        return "";
    }
    
    public static void Main()
    {
        // JSON formatı olup olmadığını kontrol etmek için ilk satırı oku
        string firstLine = Console.ReadLine();
        string elementToCheck = Console.ReadLine();
        HashSet<string> set = new HashSet<string>();
        
        // Girişin JSON dizi formatında olup olmadığını kontrol et
        if (firstLine != null && firstLine.StartsWith("[") && firstLine.EndsWith("]"))
        {
            try
            {
                // Köşeli parantezler arasındaki içeriği çıkar
                string arrayContent = firstLine.Substring(1, firstLine.Length - 2);
                
                // Tüm tırnak içindeki dizeleri eşleştirmek için regex kullan
                MatchCollection matches = Regex.Matches(arrayContent, @"""([^""]*)""");
                
                foreach (Match match in matches)
                {
                    // Yakalanan grubu (tırnaklar olmadan) ekle
                    if (match.Groups.Count > 1)
                    {
                        set.Add(match.Groups[1].Value.Trim());
                    }
                }
                
                // Elemanın JSON string formatında olup olmadığını kontrol et
                Match elementMatch = Regex.Match(elementToCheck, @"""([^""]*)""");
                if (elementMatch.Success && elementMatch.Groups.Count > 1)
                {
                    elementToCheck = elementMatch.Groups[1].Value;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error parsing JSON input: {ex.Message}");
                return;
            }
        }
        else
        {
            // Geleneksel virgülle ayrılmış girişi işle
            string[] elements = firstLine.Split(',');
            foreach (string element in elements)
            {
                set.Add(element.Trim());
            }
        }
        
        string result = ElementExists(set, elementToCheck);
        Console.WriteLine(result);
    }
}
quiz iconKendini test et

Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.

Mantık & Akış bölümündeki tüm dersler