Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Kiểm tra HashSet là tập con (subset) trong C#

Giới thiệu

Để kiểm tra một HashSet<T> có phải là tập con (subset) của một collection khác, sử dụng phương thức IsSubsetOf(). Trả về true nếu tất cả phần tử của set hiện tại đều tồn tại trong collection chỉ định.

Cú pháp

public bool IsSubsetOf (IEnumerable<T> other);

Ví dụ 1: Không phải tập con

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        HashSet set1 = new HashSet();
        set1.Add("EF");
        set1.Add("OP");

        Console.WriteLine("Phần tử trong HashSet1:");
        foreach(string val in set1) {
            Console.WriteLine(val);
        }

        HashSet set2 = new HashSet();
        set2.Add("KL");
        set2.Add("MN");
        set2.Add("OP");
        set2.Add("QR");

        Console.WriteLine("\nPhần tử trong HashSet2:");
        foreach(string val in set2) {
            Console.WriteLine(val);
        }

        Console.WriteLine("\nset1 là tập con của set2? " + set1.IsSubsetOf(set2));
    }
}

Đầu ra ví dụ 1

Phần tử trong HashSet1:
EF
OP
Phần tử trong HashSet2:
KL
MN
OP
QR
set1 là tập con của set2? False

Ví dụ 2: Là tập con

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        HashSet set1 = new HashSet();
        set1.Add("EF");
        set1.Add("OP");

        Console.WriteLine("Phần tử trong HashSet1:");
        foreach(string val in set1) {
            Console.WriteLine(val);
        }

        HashSet set2 = new HashSet();
        set2.Add("EF");
        set2.Add("OP");
        set2.Add("QR");

        Console.WriteLine("\nPhần tử trong HashSet2:");
        foreach(string val in set2) {
            Console.WriteLine(val);
        }

        Console.WriteLine("\nset1 là tập con của set2? " + set1.IsSubsetOf(set2));
    }
}

Đầu ra ví dụ 2

Phần tử trong HashSet1:
EF
OP
Phần tử trong HashSet2:
EF
OP
QR
set1 là tập con của set2? True

Các phương thức quan hệ tập hợp khác

Phương thứcÝ nghĩaVí dụ (A={1,2}, B={1,2,3})
IsSubsetOf(B)A ⊆ B (tập con, có thể bằng)True
IsProperSubsetOf(B)A ⊂ B (tập con thực sự, khác B)True
IsSupersetOf(B)A ⊇ B (tập cha, có thể bằng)False
IsProperSupersetOf(B)A ⊃ B (tập cha thực sự)False
Overlaps(B)A ∩ B ≠ ∅ (có giao nhau)True
SetEquals(B)A = B (bằng nhau)False

Đặc điểm quan trọng

  • IsSubsetOf chấp nhận bất kỳ IEnumerable<T> (List, Array, HashSet khác...).
  • Độ phức tạp: O(n) với n = Count của set hiện tại (tra cứu O(1) trong other nếu other là HashSet).
  • Set rỗng (Count == 0) là subset của mọi set (kể cả set rỗng).
  • Không thay đổi set gốc (read-only).

Ví dụ với IEnumerable khác

HashSet set = new HashSet { 1, 2, 3 };
List list = new List { 1, 2, 3, 4, 5 };
int[] array = { 1, 2, 3, 4, 5 };

Console.WriteLine(set.IsSubsetOf(list));  // True
Console.WriteLine(set.IsSubsetOf(array)); // True
Console.WriteLine(set.IsSubsetOf(new[] { 2, 3 })); // False (thiếu 1)

Ứng dụng thực tế

  • Kiểm tra quyền hạn: user permissions ⊆ required permissions.
  • Validation: input tags ⊆ allowed tags.
  • Dependency check: required packages ⊆ installed packages.
  • Feature flags: enabled features ⊆ licensed features.