• Category

    Samples a single element at random from an array.

    Time complexity: O(1)

    Space complexity: O(1)

    Type Parameters

    • T

    Parameters

    • array: ArrayLike<T>

      Array to sample element from

    Returns T | undefined

    A random element from the array or undefined if the array was empty

    Array

    const array = [1, 2, 3];

    sample(array); // 1, 2, or 3

    (shuffle:1) if you want to implement a random selection without replacement