Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an "AllIndexesOf" method? [duplicate]

Tags:

c#

.net

indexof

Background:

I'm working on an evaluator (I know there's solutions available, but I need some features that I need to implement myself). I need to find all occurrences of open brackets in the evaluation. However, for that I need all the indexes of the brackets.

Question:

Is there something like an AllIndexesOf method that returns a int[], or IEnumerable<int>?

like image 988
It'sNotALie. Avatar asked Dec 16 '22 12:12

It'sNotALie.


1 Answers

There is not but you can get all the indexes using the following LINQ query.

int number  = 10;
int[] intArray = new[] { 1, 32, 10, 5, 65, 6, 10, 10 };
var allIndexes = intArray.Select((r,i)=> new {value = r, index = i})
                         .Where(r=> r.value == number)
                         .Select(r=> r.index);

allIndexes will contain 2,6 and 7

like image 85
Habib Avatar answered Dec 18 '22 01:12

Habib