Quick way to find index i in an array[i] = someValue using Linq?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raylopez99

    Quick way to find index i in an array[i] = someValue using Linq?

    I would like to know if there's a quick "Linq" way to find the index
    of an array having a particular value. I can do this the long way by
    sequential iteration, but would like to know if there's a shortcut.

    Specifically, you have an Array, say an array of Ints. You have a
    maximum value, i.e. int someValue = Array.Max(); and you would like to
    know which ith cell of the Array holds this value.

    How to do this without iterating the entire array (and counting the
    indexes, etc, until you come across this value)?

    Of course you an also set up a map/dictionary, and your index would be
    the key, but I would like to do this for an ordinary array.

    No big deal, but I'm just trying to optimize some code (I have a 300M
    int array, and arguably the Linq way might be faster than traversing
    sequentially the array, though perhaps I'm mistaken).

    Thank you.

    RL
  • Jeroen Mostert

    #2
    Re: Quick way to find index i in an array[i] = someValue using Linq?

    raylopez99 wrote:
    I would like to know if there's a quick "Linq" way to find the index
    of an array having a particular value. I can do this the long way by
    sequential iteration, but would like to know if there's a shortcut.
    >
    Normally, that would be .Where(conditio n)...
    Specifically, you have an Array, say an array of Ints. You have a
    maximum value, i.e. int someValue = Array.Max(); and you would like to
    know which ith cell of the Array holds this value.
    >
    ....but if you're looking for the index of the maximum, that would mean going
    through the array twice (first to find the maximum, then its index), which
    is inefficient.

    Also, if you're looking for a known value in array, Array.IndexOf() beats
    ..Where().
    How to do this without iterating the entire array (and counting the
    indexes, etc, until you come across this value)?
    >
    Finding the maximum value of an arbitrary sequence (like an array)
    inherently requires examining all elements. There is no way to speed it up
    without more information (like knowing that the array is sorted, so you can
    immediately take the last or first element).

    The best you can do is only go through the array once to find both the
    maximum and its index (or rather one of its indices):

    int max = int.MinValue, maxIndex = 0;
    for (int i = 0; i != arr.Length; ++i) {
    if (arr[i] max) {
    max = arr[i];
    maxIndex = i;
    }
    }

    LINQ is of no particular help in making this clearer.
    Of course you an also set up a map/dictionary, and your index would be
    the key, but I would like to do this for an ordinary array.
    >
    No big deal, but I'm just trying to optimize some code (I have a 300M
    int array, and arguably the Linq way might be faster than traversing
    sequentially the array, though perhaps I'm mistaken).
    >
    LINQ is more expressive than manually iterating, but never more efficient
    (unless you bring things like Parallel LINQ into play). It all boils down to
    for-loops under the covers.

    --
    J.

    Comment

    • Michael C

      #3
      Re: Quick way to find index i in an array[i] = someValue using Linq?

      "raylopez99 " <raylopez99@yah oo.comwrote in message
      news:b47ae6f8-1d24-4c67-b055-a57cc72478ba@v3 0g2000hsa.googl egroups.com...
      No big deal, but I'm just trying to optimize some code (I have a 300M
      int array, and arguably the Linq way might be faster than traversing
      sequentially the array, though perhaps I'm mistaken).
      Just to add to what Jeroen said, Linq creates delegates to call functions.
      This will make it less efficient. Eg

      From item in items where item.SomeValue == 123 select item.SomeValue

      this create a function for your where clause which looks something like
      this:

      bool Where(item)
      {
      return item.SomeValue == 123;
      }

      and a similar function for the select I believe. Calling these 2 function
      will be slower than simply iterating the array.

      I think also you need to use lambda expressions (instead of straight linq)
      to get the index of an item.

      Michael


      Comment

      • Ignacio Machin ( .NET/ C# MVP )

        #4
        Re: Quick way to find index i in an array[i] = someValue using Linq?

        On Oct 25, 3:59 pm, raylopez99 <raylope...@yah oo.comwrote:
        I would like to know if there's a quick "Linq" way to find the index
        of an array having a particular value. I can do this the long way by
        sequential iteration, but would like to know if there's a shortcut.
        >
        Specifically, you have an Array, say an array of Ints. You have a
        maximum value, i.e. int someValue = Array.Max(); and you would like to
        know which ith cell of the Array holds this value.
        There is a difference if you know the value a priori or if you
        calculate it on the fly.

        Also note that the only way to do it is to sequentially traverse the
        array, either you do it or the framework will do it.
        You can use Array.Find ( ) if the value is independent of the array.

        To get the max in the other hand you will need to iterate until the
        end of the array.


        No big deal, but I'm just trying to optimize some code (I have a 300M
        int array, and arguably the Linq way might be faster than traversing
        sequentially the array, though perhaps I'm mistaken).
        is that M as in Millions?

        Comment

        • raylopez99

          #5
          Re: Quick way to find index i in an array[i] = someValue using Linq?

          On Oct 25, 1:56 pm, Jeroen Mostert <jmost...@xs4al l.nlwrote:
          Array.IndexOf()

          Thanks for this--I just found out about it after your post.

          RL

          Comment

          Working...