UniCoreFW

sorted_index_of()

Return the index of the first occurrence of `value` in `array` if `array` is sorted.

Implementation

Args: array: A list of elements that are already sorted. value: The value to search for. Returns: The index of the first occurrence of `value` in `array` if it exists, otherwise -1.

Example

sorted_index_of([1, 2, 2, 3, 4], 2)

Expected output: 1

Source Code

def sorted_index_of(array: List[T], value: T) -> int: try: return array.index(value) except ValueError: return -1