JS Built-ins
TypeScriptintermediate

Generics β€” partition<T>

πŸ“– Usage Example
partition([1, 2, 3, 4, 5], x => x % 2 === 0)
// β†’ [[2, 4], [1, 3, 5]]

partition<T> splits an array into two groups β€” those matching a predicate and those that don't.

Generics: partition<T>

Challenge: Implement partition<T>(arr: T[], pred: (x: T) => boolean): [T[], T[]] splitting into [matches, nonMatches].

partition([1,2,3,4,5], x => x % 2 === 0)  // β†’ [[2,4],[1,3,5]]
πŸ’‘ Hints (1)
  • β€’ Use a conditional push to two separate arrays, then return them as a tuple.