I have such code
senders.FirstOrDefault(sender => !sender.IsBusy);
This line is called pretty often.
The problem is that it always returns the first non-busy sender; the same first sender is returned pretty often, but the last one is returned very rarely. How to easily balance this?
Ideally, on every call I should return the most rarely used sender. I.e. between all non-busy senders select the one that was selected the least number of times during the last second.
If you want to get the least used one efficiently you will be probably good with the following non-Linq ‘list rotation’ solution, which is
O(n)effiency andO(1)space unlike most of others:Of course this adds the must-be-indexable constraint on
senderscontainer.