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");
}