Fisher-Yates (Knuth) shuffle is one of the algorithms that can be used to shuffle an array with O(n) complexity.
function shuffle (array) {
let randomIndex;
for (let currentIndex = array.length currentIndex >= 0; --currentIndex) {
1;
randomIndex = Math.floor(Math.random()
currentIndex);
[array[currentIndex], array[randomIndex]] = [ array[randomIndex],
array[currentIndex],
];
}
return array;
}