-3

Is there any lambda-based approach which will allow one to find the index of (the first) occurrence of an element in an array based on custom logic?

I know I can find the element itself using Array.First but can I find the index of that element without manually iterating the array?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • Do you mean you want to find the index of the specific element by its value? – OlegI Dec 20 '19 at 12:45
  • You mean based on a property value? There is `Array.IndexOf` for the object itself. – Johnathan Barclay Dec 20 '19 at 12:46
  • 4
    `Array.IndexOf` ? – Marc Gravell Dec 20 '19 at 12:46
  • `Array` and `List` both have convenience methods for this; see [this](https://stackoverflow.com/q/1290603/4137916) for a more general question on doing this for any `IEnumerable` (all those answers are easily extended to predicates rather than specific values, where desired). – Jeroen Mostert Dec 20 '19 at 12:48
  • If you call Array.IndexOf(), the iteration happens through the array. But if I understand the question correctly, you want the index of the first item you were looking for. But you don't want to iterate through the whole array or part of the array. Unfortunately, this is impossible if the element is not at the beginning or end of the array. This is due to the mechanism of how arrays are built. – deralbert Dec 20 '19 at 12:49
  • Does this answer your question? [Getting the index of a particular item in array](https://stackoverflow.com/questions/4388600/getting-the-index-of-a-particular-item-in-array) – Joost K Dec 20 '19 at 12:51
  • I just edited the question as I realised IndexOf is not quite adequate. – Mr. Boy Dec 20 '19 at 13:18
  • the accepted answer in the second duplicate should still satisfy your criteria. Does it? – Mong Zhu Dec 20 '19 at 14:13

4 Answers4

2

You could use Array.IndexOf(). Example:

var array = new string[] { "one", "two", "three" };
var index = Array.IndexOf(array, "two");
// index = 1
kaffekopp
  • 2,551
  • 6
  • 13
1

You can use Array.IndexOf() that "Searches for the specified object and returns the index of its first occurrence in a one-dimensional array or in a range of elements in the array." :

var foo = new int [] { 10, 42, 42, 51, 42, 100 };

Console.WriteLine(Array.IndexOf(foo, 42)); // output is 1

Although this will iterate under the hood, you don't have to manually [iterate] the array

Cid
  • 14,968
  • 4
  • 30
  • 45
1

You can do this:

var s = new string[] {"foo", "bar"};
var index = Array.FindIndex(s, s1 => s1 == "foo"); // returns 0

NOTE: it will return -1 if the element was not found

OlegI
  • 5,472
  • 4
  • 23
  • 31
  • I was sure something existed but forgot to check static members. Why I wonder is it only available that way? – Mr. Boy Dec 20 '19 at 13:23
0

There's the Array.IndexOf method.

The following code:

    String[] strings = { "the", "quick", "brown", "fox", "jumps",
                 "over", "the", "lazy", "dog", "in", "the",
                 "barn" };
    Console.WriteLine(Array.IndexOf(strings, "dog"));

Will output 8.

MSDN: https://learn.microsoft.com/en-us/dotnet/api/system.array.indexof

iqueiroz
  • 7
  • 2