Get the largest key in a dictionary
See the question and my original answer on StackOverflowIf performance is really an issue, I would create a new class on top of an existing one, implementing the standard interfaces, like this:
public class RememberMaxDictionary<K, V> : IDictionary<K, V> where K: IComparable<K>
{
private Dictionary<K, V> _inner;
public RememberMaxDictionary()
{
_inner = new Dictionary<K, V>();
}
public K MaxKey { get; private set; }
public void Add(K key, V value)
{
_inner.Add(key, value);
if (key.CompareTo(MaxKey) > 0) // test null if needed
{
MaxKey = key;
}
}
... TODO implement the rest...