Home > Blockchain >  How can I get all the indexes of the number 1 in an array?
How can I get all the indexes of the number 1 in an array?

Time:05-11

I have an array that looks like this:

int[,] figureL = {{0, 1, 0},
                  {0, 1, 0},
                  {0, 1, 1}};

How can I get the indexes of all the numbers that are equal to 1? So not just of one number but for all number 1's.

Thanks in advance!

CodePudding user response:

How about something like this:

int[,] matrix =
    {{0, 1, 0},
     {0, 1, 0},
     {0, 1, 1}};

for (var i = 0; i < matrix.GetLength(0); i  )
{
    for (var j = 0; j < matrix.GetLength(1); j  )
    {
        if (matrix[i, j] == 1)
        {
            Debug.WriteLine($"index [{i}, {j}] is 1");
        }
    }
}

The matrix.GetLength(int) calls give you the size of the two dimensions (the zero-eth and the first).

The output looks like:

index [0, 1] is 1
index [1, 1] is 1
index [2, 1] is 1
index [2, 2] is 1

CodePudding user response:

For this you will just want to iterate over both dimensions of the array and see if the value at the current index is equal to 1.

For your two dimensional array you defined here, you'll need to find the Rank (which shows you how many dimensions there are in the array). You'll also need to find the Upper Bound of each of those dimensions by calling figureL.GetUpperBound(rank), where rank is the current dimension. Then you can use that information to construct your loops which will iterate over your data structure.

Here is an example of two dimensional array enumeration using your syntax above:

        private void Test()
        {
            int[,] figureL= {
                {0,1,0 },
                {0,1,0 },
                {0,1,1 }
            };

            Parse(figureL);
        }

        private void Parse(int[,] input)
        {
            var dimension1 = input.GetUpperBound(0);
            var dimension2 = input.GetUpperBound(1);

            for (int i = 0; i <= dimension1; i  )
            {
                for (int j = 0; j <= dimension2; j  )
                {
                    if (input[i, j] == 1)
                        Console.WriteLine($"[{i},{j}]");
                }
            }
        }

Of course, if you are dealing with arrays with more than two dimensions, you'll have to change this, but as it stands it should be enough to get you started with your multidimensional arrays.

CodePudding user response:

You can write a LINQ-like extension method that helps you finding arbitray elements in an array:

public static IEnumerable<(int,int)> FindIndices<T>(this T[,] source, Func<T,bool> predicate)
    {
        for (int i = 0; i < source.GetLength(0); i  )
        {
            for (int j = 0; j < source.GetLength(1); j  )
            {
                if(predicate.Invoke(source[i,j]))
                {
                     yield return (i,j);
                }
            }
        }
    }

It will returns tuples of the indices of the elements that fullfill the conditions giving in predicate. Usage is

var indices = figureL.FindIndices(x => x == 1);

Online-demo: https://dotnetfiddle.net/tdcwa4

  •  Tags:  
  • c#
  • Related