| 1 | /**
|
|---|
| 2 | * Finds the index of the last occurrence of a value in a sorted array.
|
|---|
| 3 | * This function is similar to `Array#lastIndexOf` but is specifically designed for sorted arrays.
|
|---|
| 4 | *
|
|---|
| 5 | * Make sure to provide a sorted array to this function, as it uses a binary search to quickly find the index.
|
|---|
| 6 | *
|
|---|
| 7 | * @param {ArrayLike<T> | null | undefined} array The sorted array to inspect.
|
|---|
| 8 | * @param {T} value The value to search for.
|
|---|
| 9 | * @returns {number} Returns the index of the last matched value, else -1.
|
|---|
| 10 | *
|
|---|
| 11 | * @example
|
|---|
| 12 | * const numbers = [1, 2, 3, 4, 5];
|
|---|
| 13 | * sortedLastIndexOf(numbers, 3); // Return value: 2
|
|---|
| 14 | * sortedLastIndexOf(numbers, 6); // Return value: -1
|
|---|
| 15 | *
|
|---|
| 16 | * // If the value is duplicated, it returns the last index of the value.
|
|---|
| 17 | * const duplicateNumbers = [1, 2, 2, 3, 3, 3, 4];
|
|---|
| 18 | * sortedLastIndexOf(duplicateNumbers, 3); // Return value: 5
|
|---|
| 19 | *
|
|---|
| 20 | * // If the array is unsorted, it can return the wrong index.
|
|---|
| 21 | * const unSortedArray = [55, 33, 22, 11, 44];
|
|---|
| 22 | * sortedLastIndexOf(unSortedArray, 11); // Return value: -1
|
|---|
| 23 | *
|
|---|
| 24 | * // -0 and 0 are treated the same
|
|---|
| 25 | * const mixedZeroArray = [-0, 0];
|
|---|
| 26 | * sortedLastIndexOf(mixedZeroArray, 0); // Return value: 1
|
|---|
| 27 | * sortedLastIndexOf(mixedZeroArray, -0); // Return value: 1
|
|---|
| 28 | *
|
|---|
| 29 | * // It works with array-like objects
|
|---|
| 30 | * const arrayLike = { length: 3, 0: 10, 1: 20, 2: 20 };
|
|---|
| 31 | * sortedLastIndexOf(arrayLike, 20); // Return value: 2
|
|---|
| 32 | */
|
|---|
| 33 | declare function sortedLastIndexOf<T>(array: ArrayLike<T> | null | undefined, value: T): number;
|
|---|
| 34 |
|
|---|
| 35 | export { sortedLastIndexOf };
|
|---|