IndexOfAll
Returns all indices of n in an IList.
- Use
Enumerable.Range()to iterate over all indices indata. - Use
Enumerable.Where()in combination withobject.Equals()to compare each value indatatonand return only matching indices.
using System.Collections.Generic;
using System.Linq;
public static partial class _30s
{
public static IEnumerable<int> IndexOfAll<T>(IList<T> data, T n)
{
return Enumerable
.Range(0, data.Count())
.Where(i => object.Equals(n, data[i]));
}
}int[] nums = {1, 2, 4, 5, 2, 2, 4};
_30s.IndexOfAll(_30s.IndexOfAll(nums, 2)); // {1, 4, 5}