473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Oct 25 '08 #1
4 9959
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stephen Travis | last post by:
I'm trying to write a subroutine that will fill an array of some type with several objects of that type. The subroutine works fine if I directly reference the array but if I pass the array as an...
7
by: jimi_xyz | last post by:
I am kind of new to C sharp; I have a quick question I have multiple text boxes 12 of them to be exact. What I want to do is fill them with values that are in a array of integers. The way I am...
14
by: rocketman768 | last post by:
Man, that title should be in a poem, but anyways...So, I have this program, and it has an array of 40 million elements. The problem is that I have a for loop that continually is doing stuff to this...
1
by: Acrylic | last post by:
Is it possible to add columns to an array using dimenion of another array ? For example i have an array.. int anArray = new int, Obviously.. it accepts 20 columns. What I want to do is to use...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
4
by: Arthur Dent | last post by:
Hello all, I am trying to figure out how to do something in LINQ, IF I can even do it in LINQ. Online samples and such for LINQ is still a bit on the sketchy side. Here is what I want to...
22
by: Steve Richter | last post by:
Does the .NET framework provide a class which will find the item in the collection with a key which is closest ( greater than or equal, less than or equal ) to the keys of the collection? ex:...
10
by: Bob Bedford | last post by:
Hi all, it's there any simple way to create an array of years and months dynamically (2 loops) for an associative array ? I've tried this so far: (having an error on last line with the "=>"...
0
by: Seb | last post by:
DataClassesDataContext db = new DataClassesDataContext(); SimpleProgramsHasExercise sphe = new SimpleProgramsHasExercise(); int exerciseId = (int)ExercisesGridView.DataKeys.Values; int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.