hashset是高性能无序列表,时间复杂度为O(1)
using System;
using System.Collections.Generic;
namespace ConsoleApp13
{
class Program
{
static void Main(string[] args)
{
HashSet<string> setA = new HashSet<string>() { "A", "B", "C" };
HashSet<string> setB = new HashSet<string>() { "A", "B", "C","x" };
HashSet<string> setC = new HashSet<string>() { "A", "B", "C", "D","E" };
HashSet<string> setD = new HashSet<string>() { "A", "X", "Y", "Z", "E" };
//子集判断
if (setA.IsProperSubsetOf(setC))
{
Console.WriteLine("setA是setC的子集");
}
//合集,新的集合是setA
setA.UnionWith(setB);
//交集,都存在的,新的集合是setA
setA.IntersectWith(setD);
//差集,setA中存在,setD中不存在,新的集合是setA
setA.ExceptWith(setD);
//异或,两个集合中不都有的,setA中存在,setB中不存在,和setB中存在,setA中不存在的
setA.SymmetricExceptWith(setB);
}
}
}