Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Value in Multidimensional Array and return all items c#

Tags:

arrays

c#

.net

here is the issue, I have an array defined like below:

int[,] Users = new int[1000,3];

Its data will be something like:

  • 0,1,2
  • 1,2,1
  • 2,3,2
  • 3,3,4
  • 4,2,3 ...

the array is used as needed by my script. but I need to be able to filter the array based on one of its dimensions, and return all available matches.

For example, filtering on dimension [1], wanting all that match '3' will return an array containing:

  • 2,3,2
  • 3,3,4

Could anyone give me a hand with this?

Many thanks.

like image 735
MWard Avatar asked Apr 10 '11 14:04

MWard


People also ask

How do you find the value of multidimensional array?

Size of multidimensional arrays: The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int[][] x = new int[10][20] can store a total of (10*20) = 200 elements.

What is the output of C programming with multidimensional array?

6) What is the output of C Program with arrays.? Explanation: 0 0 is the answer.

Can you pass multidimensional array to a function?

To pass multidimensional arrays to a function, only the name of the array is passed to the function (similar to one-dimensional arrays).

What is the syntax for multidimensional array in C?

The basic form of declaring a two-dimensional array of size x, y: Syntax: data_type array_name[x][y]; Here, data_type is the type of data to be stored.


1 Answers

If you can change your array from int[,] to int[][] then you can easily achieve this using LINQ.

  int[][] users = new int[][]
  {
    new int[]{0,1,2},
    new int[]{1,2,1},
    new int[]{2,3,2},
    new int[]{3,3,4},
    new int[]{4,2,3}
  };


  var result = from u in users 
               where u[1] == 3 
               select u;

If changing your array is not an option then you could write a Filter function as follows.

public static IEnumerable<T[]> Filter<T>(T[,] source, Func<T[], bool> predicate)
{
  for (int i = 0; i < source.GetLength(0); ++i)
  {
    T[] values = new T[source.GetLength(1)];
    for (int j = 0; j < values.Length; ++j)
    {
      values[j] = source[i, j];
    }
    if (predicate(values))
    {
      yield return values;
    }
  }      
}

The above could then be called as follows

var result = Filter(users, u => u[1] == 3);

You could take this a step further and implement your own custom Linq extension for the Where function which would allow you to filter the T[,] arrays. Here is a naive example that could get you started.

public static class LinqExtensions
{
  public static IEnumerable<T[]> Where<T>(this T[,] source, Func<T[], bool> predicate)
  {
    if (source == null) throw new ArgumentNullException("source");
    if (predicate == null) throw new ArgumentNullException("predicate");
    return WhereImpl(source, predicate);
  }

  private static IEnumerable<T[]> WhereImpl<T>(this T[,] source, Func<T[], bool> predicate)
  {      
    for (int i = 0; i < source.GetLength(0); ++i)
    {
      T[] values = new T[source.GetLength(1)];
      for (int j = 0; j < values.Length; ++j)
      {
        values[j] = source[i, j];
      }
      if (predicate(values))
      {
        yield return values;
      } 
    }
  }    
}

With this you can again use Linq as in the first example to filter the array

  var result = from u in users 
               where u[1] == 3 
               select u;
like image 196
Chris Taylor Avatar answered Oct 25 '22 23:10

Chris Taylor