C # determines that a collection is a subset of another collection

Source: Internet
Author: User

There are two of these collections:

string[] Bigarr = new string[] {"A", "B", "C"};
string[] Smallarr = new string[] {"A", "B"};

Now you need to determine if Smallarr is a subset of Bigarr. As long as holding the Bigarray and Smallarr comparison, the difference set, if the number of difference set is greater than 0, it means that Smallarr is a subset of Bigarr.

            //On the basis of large sets, the difference sets of large sets are obtained according to the small set            varExceptarr =bigarr.except (Smallarr); //determine if it is a subset            if(Exceptarr.any ()) {Console.WriteLine ("Samllarr is a subset of the Bigarr"); }            Else{Console.WriteLine ("Samllarr is not a subset of Bigarr"); }

In this way, you can only judge if it is a subset, that is, a subset of the set element that is always smaller than the large one.

Sometimes, there is the need to determine whether Bigarr contains Smallarr, that is, Smallarr can be a subset of Bigarr, or it can be the same as Bigarr.

            // determines whether a subset or 2 sets are the same            if (Smallarr.all (t = bigarr.any (b = b==t)))            {                Console.WriteLine ("Samllarr is a subset or the same as Bigarr ")            ;            } Else             {                Console.WriteLine ("Samllarr is not a subset or the same as Bigarr ");            } 

C # determines that a collection is a subset of another collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.