Get Middle Element
Less than 1 minuteTC-Medium
Get Middle Element
Problem
Get the middle element of the array by implementing a GetMiddleElement method, the result should be returned in an array format.
GetMiddleElement<[1, 2, 3, 4, 5]> // [3]
GetMiddleElement<[1, 2, 3, 4, 5, 6]> // [3, 4] (two middle elements for even length)Solution
Approach: Peel Both Ends Simultaneously
Remove one element from the front and one from the back each step. When 1 or 2 elements remain, that's the middle.
type GetMiddleElement<T extends unknown[]> =
T extends [infer _First, ...infer Middle, infer _Last]
? Middle extends []
? T // 2 elements left — both are middle
: GetMiddleElement<Middle>
: T // 0 or 1 element — that's the middleHow it works:
- Pattern match
[First, ...Middle, Last]to peel first and last. - If
Middleis empty, bothFirstandLastremain — returnT(the 2-element tuple). - Otherwise, recurse on
Middle. - If
Thas 0 or 1 elements (can't destructure both ends), returnTas-is.
Key Takeaways
- TypeScript's rest-in-the-middle pattern
[infer F, ...infer M, infer L]is a powerful way to peel both ends at once. - The invariant "middle is empty means exactly 2 elements left" works because we only reach this branch with ≥ 2 elements.
