A simple method of determining whether a set is a subset of another set in C # _c# tutorial

Source: Internet
Author: User

To see this title, we first thought of looping through one of the arrays, to determine whether each element in the array appears in another array, so that the array is not a subset of another array, but this is too complicated, there is no simple way to do?

For example, there are two sets of these:

Copy Code code as follows:
string[] Bigarr = new string[] {"A", "B", "C"};
string[] Smallarr = new string[] {"A", "B"};

Now you need to determine whether Smallarr is a subset of Bigarr. Just hold the Bigarr and Smallarr comparison, the difference set, if the number of difference sets is greater than 0, it shows that Smallarr is a subset of Bigarr.

Copy Code code as follows:
On the basis of large sets, the difference sets of large sets are obtained based on small sets
var Exceptarr = bigarr.except (Smallarr);
Determine if it is a subset
if (Exceptarr.any ())
{
Console.WriteLine ("Samllarr is a subset of Bigarr");
}
Else
{
Console.WriteLine ("Samllarr is not a subset of Bigarr");
}

The above method can only determine whether a subset, that is, the subset of the set element is always smaller than the large set.

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

Copy Code code as follows:
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 same of Bigarr");
}
Else
{
Console.WriteLine ("Samllarr is not a subset or the same as Bigarr");
}

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.