Check whether an array is a subset of another

后端 未结 10 1048
走了就别回头了
走了就别回头了 2020-11-22 16:17

Any idea on how to check whether that list is a subset of another?

Specifically, I have

List t1 = new List { 1, 3, 5 };
L         


        
10条回答
  •  一整个雨季
    2020-11-22 16:49

    @Cameron's solution as an extension method:

    public static bool IsSubsetOf(this IEnumerable a, IEnumerable b)
    {
        return !a.Except(b).Any();
    }
    

    Usage:

    bool isSubset = t2.IsSubsetOf(t1);
    

    (This is similar, but not quite the same as the one posted on @Michael's blog)

提交回复
热议问题