Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fyhhub authored Sep 12, 2023
1 parent 8e91345 commit 77f203d
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 实现生成一个长度为 n 的不重复随机数组

```js
const getRandomArr2 = function (arr, n) {
const result = [];
const map = new Map();
while (result.length < n) {
// 生成随机数
const randomNum = arr[Math.floor(Math.random() * arr.length)];
if (!map.has(randomNum)) {
map.set(randomNum, randomNum);
result.push(randomNum);
}
}
return result;
}
```

0 comments on commit 77f203d

Please sign in to comment.