The main idea is to use binary search to find the rightmost value that equal to current value.

And, for the upper bound of search range, we don't need to set it as the end of the array, instead, we can double the length of range each time, until it's unequal to current value.