424,054 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,054 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Oct 25 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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(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[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.
Oct 25 '08 #2

P: n/a
"raylopez99" <ra********@yahoo.comwrote in message
news:b4**********************************@v30g2000 hsa.googlegroups.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
Oct 25 '08 #3

P: n/a
On Oct 25, 3:59 pm, raylopez99 <raylope...@yahoo.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?

Oct 27 '08 #4

P: n/a
On Oct 25, 1:56*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
Array.IndexOf()

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

RL
Oct 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.