I assume that you do not want to use additional memory and do not want to exceed the algorithmic complexity of the basic iteration method, therefore ToList, GroupBy, IndexOf are not described in my proposed implementations.
In addition, in order not to place restrictions on the type of element, I use predicates.
public static class EnumerableExtensions {
These methods can be used as extension methods:
new[]{"apple", "orange", "banana", "pineapple"}.GetRange(i => i == "orange", i => i == "banana")
source share