Get a random element from array

Typescript

Get a random element from array - O(1)

ts

export function randomElement<T>(array: T[]): T { return array[Math.floor(Math.random() * array.length)]; }