How can I get all array/list values within a given range?

Given a sorted array/list/hashtable/dictionary/sortedlist/etc of values, what would be the best way to derive a selection of all the values between two values?

var currentTime = 10;

var allTimes = [1,2,4,7,8,9,11,14,16,17,17,19,22,28,46];
// Returns a new list with [11,14,16,17,17,19]
var nextFewTimes = GetListWithinRange(allTimes, currentTime, currentTime+10);

The answer depends on what kind of container you are using, but if it’s a simple linear container like a list or array, then just use a for loop. It’s linear time (O(N)), which is totally fine unless this array is really enormous and GetListWithinRange is run a lot.

Given a sorted list or array use BinarySearch to get the lowest and highest values.

The two searches will be O(logN), presuming you can leave the data in situ then there would be no need to read the individual values until processing was required.