The optimal solution to shuffle an Array in JavaScript
1–3 of 3 posts
Re: The optimal solution to shuffle an Array in JavaScript
#2Surprised Array.sort wasn't mentioned.
[1,2,3,4,5,6,7,8,9,0].sort(()=>Math.random()*2-1)
Re: The optimal solution to shuffle an Array in JavaScript
#3Surprised Array.sort wasn't mentioned. [1,2,3,4,5,6,7,8,9,0].sort(()=>Math.random()*2-1)
Nice one there!
sort is an elegant solution even tho the time complexity may be O(nlogn).