Is there any way to find minimum value index more efficient/faster than this?
int minimumValueIndex = List.IndexOf(List.Min());
Yes, you can remove the overhead of List.IndexOf()
by building a custom Min()
extension. (Really, Enumerable.Min()
should have an extension that selects the original element by key instead of selecting a transformation. This oversight is particularly painful in situations like this.)
public static int IndexOfMin(this IList<int> self)
{
if (self == null) {
throw new ArgumentNullException("self");
}
if (self.Count == 0) {
throw new ArgumentException("List is empty.", "self");
}
int min = self[0];
int minIndex = 0;
for (int i = 1; i < self.Count; ++i) {
if (self[i] < min) {
min = self[i];
minIndex = i;
}
}
return minIndex;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With