题解 | #无重复数组#
无重复数组
https://www.nowcoder.com/practice/d2fa3632268b41df9bc417b74802ad8c
const _getUniqueNums = (start, end, n) => { // 结果数组 const result = []; for (let index = 0; index < n; index++) { // start ~ end 之间的随机数 let randomNum = Math.floor(Math.random() * (end - start)) + start; if (result.includes(randomNum)) { // 如果当前result数组中包含随机数则直接index-1重新循环 index--; } else { // 如果当前result数组中不包含随机数则直接push进去 result.push(randomNum); } } return result; };