Check whether an array is a subset of another

后端 未结 10 920
走了就别回头了
走了就别回头了 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 17:10

    Use HashSet instead of List if working with sets. Then you can simply use IsSubsetOf()

    HashSet t1 = new HashSet{1,3,5};
    HashSet t2 = new HashSet{1,5};
    
    bool isSubset = t2.IsSubsetOf(t1);
    

    Sorry that it doesn't use LINQ. :-(

    If you need to use lists, then @Jared's solution works with the caveat that you will need to remove any repeated elements that exist.

提交回复
热议问题