简体   繁体   中英

Check if all elements from one array are in the second array c#

How can I check if all the elements from one array are in another array? I have a 2d array with 3 arrays in it,and I want to check all of those 3 arrays if they have all the elements from allnumbers. array1=allnumbers ? array2=allnumbers ? array1=allnumber2 ? I need to return true if at least one contains all the elements from allnumbers. I have the code bellow,but I need it not to contain more than 3 control flow statements.

// int[,][] array = {array1, array2, array3}
static bool CheckLine(int[,][] array)
        {
            const int maxL = 9;
            bool result = false;
            int[] allnumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            foreach (var singlearray in array)
            {
                int[] arr = singlearray;
                int p = 0;
                foreach (var num in allnumbers)
                {
                    foreach (var arraynumber in singlearray)
                    {
                        if (arraynumber == num)
                        {
                            p++;
                        }
                    }

                    if (p == maxL)
                    {
                        result = true;
                        break;
                    }
                }
            }

            return result;
        }

If the values in your array are unique, and you don't care about the order they're in, this is a job for HashSet . (In other words, if your arrays contain sets of numbers you can treat them as sets.) Here's the basic outline of comparing sets.

var allnumbersSet = new HashSet<int>(allnumbers);
var allnumbers2Set= new HashSet<int>(allnumbers2);

if (allnumbersSet.IsSupersetOf(allnumbers2Set)) {
   /* everything in allnumbers2 is also in allnumbers1 */
}

The people who put together DotNet did a really good job creating and optimizing those collection classes; you can use them with confidence to get good performance.

It seems, that you have two-dimensional jagged array . You can simplify your code by using Except and check the difference between allnumbers array and single row at every loop iteration.

static bool CheckLine(int[,][] array)
{
    int[] allnumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    foreach (var singlearray in array)
    {
        var diff = allnumbers.Except(singlearray);
        if (!diff.Any())
        {
            return true;
        }
    }

    return false;
}

If there is no elements in a difference, it'll mean that single item from source 2D array has all elements from allnumbers array.

Example of the usage

var array = new int[2, 2][];
array[0, 0] = new[] { 1, 2, 8 };
array[0, 1] = new[] { 3, 4, 5, 6 };
array[1, 1] = new[] { 3, 2, 1, 4, 5, 7, 6, 10, 9, 8 };
array[1, 0] = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

CheckLine(array);

The last two items satisfy the condition, execution will break and return true for { 3, 2, 1, 4, 5, 7, 6, 10, 9, 8 } array. Also don't forget to add using System.Linq directive at the top of file

Thank you for your help. I forgot to mention that I can use only "using System;"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM