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

  • Thread starter Thread starter raylopez99
  • Start date Start date
R

raylopez99

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
 
raylopez99 said:
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(condition)...
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 > max) {
max = arr;
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.
 
raylopez99 said:
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
 
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?
 
Back
Top