【发布时间】:2012-11-18 12:03:29
【问题描述】:
我有两个 HashSet——setA 和 setB。
- 如何找到 setA 和 setB 的补集?
- 交叉路口的代码是找到交叉路口的最佳方法吗?
代码
string stringA = "A,B,A,A";
string stringB = "C,A,B,D";
HashSet<string> setA = new HashSet<string>(stringA.Split(',').Select(t => t.Trim()));
HashSet<string> setB = new HashSet<string>(stringB.Split(',').Select(t => t.Trim()));
//Intersection - Present in A and B
HashSet<string> intersectedSet = new HashSet<string>( setA.Intersect(setB));
//Complemenet - Present in A; but not present in B
更新:
使用OrdianlIgnoreCase 忽略大小写敏感How to use HashSet<string>.Contains() method in case -insensitive mode?
参考:
【问题讨论】:
-
“我们如何找到setA和setB的补集?”不就是
setA.Except(setB)吗?