We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第 107 题:考虑到性能问题,如何快速从一个巨大的数组中随机获取部分元素 比如有个数组有100K个元素,从中不重复随机选取10K个元素。
法一: 由于随机从100K个数据中随机选取10k个数据,可采用统计学中随机采样点的选取进行随机选取,如在0-50之间生成五个随机数,然后依次将每个随机数进行加50进行取值,性能应该是最好的。
法二: 快速生成一个巨大数组 使用Array.from() 通过Set特性,存放随机数,这里需要注意的是,没有就add,有就递归,总之要保证遍历的每一项都要找到一个唯一随机值,如果有就跳过就不能保证最后能获取到10k个值。
const randomNumHandle = (len, randomNum) => { // 快速生成一个有len个元素的巨大数组 let originArr = Array.from({length: len}, (v, i) => i); let resultSet = new Set() // 快速选取randomNum个元素 for(let i = 0; i < randomNum; i++) { addNum(resultSet, originArr) } function addNum () { let luckDog = Math.floor(Math.random() * (len - 1)) if(!resultSet.has(originArr[luckDog])) { resultSet.add(originArr[luckDog]) } else { addNum() } } return Array.from(resultSet) } // 比如有个数组有100K个元素,从中不重复随机选取10K个元素 console.log(randomNumHandle(100000, 10000))
(10000) [27784, 66572, 27315, 40156, 96729, 58756, 30823, 23521, 29948, 30964, 93575, 8250, 84128, 56055, 50914, 22715, 51331, 76186, 9374, 62607, 69400, 56221, 88673, 58384, 75880, 15955, 83055, 80018, 58629, 87050, 58973, 27608, 10643, 3032, 46829, 54258, 60324, 23041, 86465, 34973, 97064, 99430, 66246, 4608, 29317, 2035, 97926, 31838, 98485, 24692, 66996, 91420, 12390, 27324, 44748, 45744, 76575, 12494, 75592, 70476, 24683, 74219, 14897, 83863, 88356, 35288, 38732, 1875, 50741, 39930, 11173, 23535, 45221, 96828, 42283, 41511, 81011, 63434, 4375, 40234, 16998, 37468, 68825, 7407, 27391, 47692, 21628, 42445, 43379, 10218, 55863, 91489, 34772, 64601, 77545, 4481, 91932, 65603, 69958, 81040, …]
default: 30.98388671875ms
The text was updated successfully, but these errors were encountered:
No branches or pull requests
第 107 题:考虑到性能问题,如何快速从一个巨大的数组中随机获取部分元素
比如有个数组有100K个元素,从中不重复随机选取10K个元素。
法一:
由于随机从100K个数据中随机选取10k个数据,可采用统计学中随机采样点的选取进行随机选取,如在0-50之间生成五个随机数,然后依次将每个随机数进行加50进行取值,性能应该是最好的。
法二:
快速生成一个巨大数组 使用Array.from()
通过Set特性,存放随机数,这里需要注意的是,没有就add,有就递归,总之要保证遍历的每一项都要找到一个唯一随机值,如果有就跳过就不能保证最后能获取到10k个值。
(10000) [27784, 66572, 27315, 40156, 96729, 58756, 30823, 23521, 29948, 30964, 93575, 8250, 84128, 56055, 50914, 22715, 51331, 76186, 9374, 62607, 69400, 56221, 88673, 58384, 75880, 15955, 83055, 80018, 58629, 87050, 58973, 27608, 10643, 3032, 46829, 54258, 60324, 23041, 86465, 34973, 97064, 99430, 66246, 4608, 29317, 2035, 97926, 31838, 98485, 24692, 66996, 91420, 12390, 27324, 44748, 45744, 76575, 12494, 75592, 70476, 24683, 74219, 14897, 83863, 88356, 35288, 38732, 1875, 50741, 39930, 11173, 23535, 45221, 96828, 42283, 41511, 81011, 63434, 4375, 40234, 16998, 37468, 68825, 7407, 27391, 47692, 21628, 42445, 43379, 10218, 55863, 91489, 34772, 64601, 77545, 4481, 91932, 65603, 69958, 81040, …]
default: 30.98388671875ms
The text was updated successfully, but these errors were encountered: