Samples a single element at random from an array.
Time complexity: O(1)
Space complexity: O(1)
Array to sample element from
A random element from the array or undefined if the array was empty
undefined
Array
const array = [1, 2, 3];sample(array); // 1, 2, or 3 Copy
const array = [1, 2, 3];sample(array); // 1, 2, or 3
(shuffle:1) if you want to implement a random selection without replacement
Samples a single element at random from an array.
Time complexity: O(1)
Space complexity: O(1)