9

Say I have an array like this

  string [] fruits = {"watermelon","apple","apple","kiwi","pear","banana"};

Is there an built in function that allows me to query all the index of "apple" ? For example,

  fruits.FindAllIndex("apple"); 

will return an array of 1 and 2

If there is not, how should I implement it?

Thanks!

1

3 Answers 3

12

LINQ version:

var indexes = fruits.Select((value, index) => new { value, index })
                    .Where(x => x.value == "apple")
                    .Select(x => x.index)
                    .ToList();

Non-LINQ version, using Array<T>.IndexOf() static method:

var indexes = new List<int>();
var lastIndex = 0;

while ((lastIndex = Array.IndexOf(fruits, "apple", lastIndex)) != -1)
{
    indexes.Add(lastIndex);
    lastIndex++;
}
Sign up to request clarification or add additional context in comments.

4 Comments

@PaulNikonowicz Sorry I'm very new at Linq, could you explain what do you mean by lazy? Thanks!
If you remove the .ToList() in the first method, your function will be lazy. This means if you ran indexes.Take(2), the function can stop executing once two indexes have been found, rather than finding ALL indexes first, then removing all but the first two.
ToList doesn't make it finding all indexes first, he still stop when the first two item are found... ToList just make the query run now instead of being deferred
@FabioMarcolini how to be case insensitive?
9

One way would be to write like this:

var indices = fruits
                .Select ((f, i) => new {f, i})
                .Where (x => x.f == "apple")
                .Select (x => x.i);

Or the traditional way:

var indices = new List<int>();
for (int i = 0; i < fruits.Length; i++)
    if(fruits[i] == "apple")
        indices.Add(i);

4 Comments

@PaulNikonowicz And that's bad - why?
I don't think he's saying that's bad, just providing additional information.
@Magnus it works great! thanks! But I don't understand how the first SELECT knows 'f' is the content of the object and 'i' is the index? Thanks!
@Fever it is using this select function where the selector has both the object and the index.
0

Pretty easy with an extension method.

var fruits = new[] { "watermelon","apple","apple","kiwi","pear","banana" };
var indexes = fruits.FindAllIndexes("apple");

public static class Extensions
{
  public static int[] FindAllIndexes(this string[] array, string search) => array
      .Select((x, i) => (x, i))
      .Where(value => value.x == search)
      .Select(value => value.i)
      .ToArray();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.