This AgeList keeps track of the best record for each age, and then ignores ages that do not have agorists when asked for winners.
While not all players are removed on the Insert (unsure if this is a strong requirement), this should save time in general by being lazy. The biggest weakness is the call for OrderBy. If this view is too expensive and space is available, spring for SortedList could hold all inserted a.
If space is at its best when time is available, simply write a cleaning method similar to GetWinners. You can even call from InsertMethod to clean up after each insert.
public class AgeList<T, U> where T:IComparable<T> where U:IComparable<U> { Dictionary<T, U> store = new Dictionary<T, U>(); public void Insert(T a, U b) { if (!store.ContainsKey(a) || store[a].CompareTo(b) < 0) { store[a] = b; } //else discard by doing nothing } public IEnumerable<KeyValuePair<T, U>> GetWinners() { bool first = true; U record = default(U); foreach(T key in store.Keys.OrderBy(t => t)) { U newValue = store[key]; if (first) { first = false; record = newValue; yield return new KeyValuePair<T, U>(key, record); } else if (record.CompareTo(newValue) < 0) { record = newValue; yield return new KeyValuePair<T, U>(key, record); } } } }
source share