partitionArray Function
Partitions an array in-place according to some criterion, such that elements that fulfill the criterion are grouped in the lower
portion of the array, and those that fail to fulfill the criterion are grouped in the upper portion of the array.
partitionArray<T>(array: T[], criterion: (element: T) => boolean): number
note The relative ordering of elements within each partition is unspecified. Example:
function isEven(n: number) { return 0 === n % 2; }
const list = [ 1, 2, 3, 4, 5 ];
const firstOddIndex = partitionArray(list, isEven); // firstOddIndex = 2
// 2 and 4 now appear before 1, 3, and 5 in the list; their ordering is otherwise unspecified.
for (let i = 0; i < list.length; i++)
assert(isEven(list[i]) === i < firstOddIndex);
Returns - number
The index of the upper partition, i.e., of the first element that fails the criterion. If all elements fulfill the criterion, this is the length of the array.
Defined in
Last Updated: 20 June, 2023