[Mono-list] Minimum element of a list

Jonathan Pryor jonpryor at vt.edu
Fri Jan 7 17:01:08 EST 2011


On Jan 7, 2011, at 4:40 PM, David Henderson wrote:
> I have a list and I would like to find the minimum element in the array from the 
> current iterator in a foreach loop to the end, not the minimum element overall.

This isn't strictly possible, as there's no way of knowing the index of the current iterator element.  You could use List<T>.IndexOf() to find it, but this would return the first matching element, which doesn't help if there are multiple matching elements.

Consequently, I would suggest using a normal `for` loop instead of a `foreach` loop, at which point you will have access to the index, allowing use of Enumerable.Skip() and Enumerable.Min() extension methods:

	List<double> d = ...;
	for (int i = 0; i < d.Count; ++i) {
		double min_e = d.Skip (i).Min ();
	}

If you absolutely know you won't have duplicates, you could use a `foreach` loop and use List<T>.IndexOf(), but the performance of that would not be great...

 - Jon



More information about the Mono-list mailing list