1

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;
        }
1
  • Have you tried to use Except method? Please, also share the int[,][] array sample Commented Mar 27, 2020 at 15:04

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I think it's better to leave allnumbers2 as is since it's unnecessary for argument of IsSupersetOf to be HashSet.
So basically you want some kind of ienumerable to just walk through and hashset to check if it's there
1

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

Comments

0

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

2 Comments

I guess, it should be a comment, not the answer. You can add using System.Linq; and using System.Collections.Generic to make the answers above working
I saw its working with using System.Linq; , but Im not allowed to use it. I need to use only "using System;"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.