简体   繁体   English

如何替换数组中的重复项

[英]How to replace duplicates from array

What is the way to remove duplicates from an array that generates random numbers?从生成随机数的数组中删除重复项的方法是什么? I've made random number generator that allows the user to type min/max values and how many numbers to generate.我制作了随机数生成器,允许用户输入最小值/最大值以及生成多少个数字。 I also removed duplicates but if the user types 5 numbers to generate and there is 1 duplicate it will generate 4 numbers.我还删除了重复项,但如果用户键入 5 个数字来生成并且有 1 个重复项,它将生成 4 个数字。 How to add another number to replace the duplicate?如何添加另一个数字来替换重复的数字?

 <html> <head> </head> <body> <div id="container"> <label for="fname">Min:</label> <input type="text" id="Min"><br><br> <label for="lname">Max:</label> <input type="text" id="Max"><br><br> <label for="size">Generate:</label> <input type="text" id="size"> <label for="size">numbers</label> <br><br> <label for="Sort">Sort numbers:</label> <select id="Sort"> <option value="Do not sort">Do not sort</option> <option value="Ascending">Ascending</option> <option value="Descending">Descending</option> </select> <br><br> <label for="Duplicates">Allow Duplicates:</label> <select id="Duplicates"> <option value="No">No</option> <option value="Yes">Yes</option> </select> <br><br> <button onclick="func()">Generate</button> <br><br> <span id="result"></span> </div> </body> <script> function func() { //Inputs var min = parseInt(document.getElementById('Min').value); var max = parseInt(document.getElementById('Max').value); var sort = document.getElementById('Sort'); var duplicate = document.getElementById('Duplicates'); var size = document.getElementById('size').value; var result = document.getElementById('result'); const rand = []; for (var i = 0; i <= size - 1; i++) { rand.push(Math.floor(Math.random() * max) + min); } if (sort.value == "Ascending") { rand.sort(function(a, b) { return a - b }); result.innerHTML = rand; let unique = [...new Set(rand)]; console.log(unique) } else if (sort.value == "Descending") { rand.sort(function(a, b) { return b - a }); result.innerHTML = rand; } else if (sort.value == "Do not sort") { result.innerHTML = rand; } } </script> </html>

As you know, a Set doesn't allow duplicate values:如您所知, Set不允许重复值:

 let ary = [1,1,2,2,3,3,4,4,5,5]; let set = new Set(ary); let newAry = Array.from(set); console.log(newAry); // outputs: [1,2,3,4,5]

You can get a set to contain the desired amount of numbers by using its size property:你可以通过使用它的size属性来获取一个包含所需数量的集合:

 function randomNumberBetween(iLowest,iHighest) { var iChoices = iHighest - iLowest + 1; return Math.floor(Math.random() * iChoices + iLowest); } let max = 10; let min = 1 let amountOfNumbers = max - min; let set = new Set(); while (set.size < amountOfNumbers) { set.add(randomNumberBetween(min,max)); } console.log(Array.from(set));

However, it is more efficient to randomize an already existing array, because it avoids all the random duplicates rejected by the set:但是,对已经存在的数组进行随机化会更有效,因为它避免了集合拒绝的所有随机重复项:

 function randomNumberBetween(iLowest,iHighest) { var iChoices = iHighest - iLowest + 1; return Math.floor(Math.random() * iChoices + iLowest); } function randomizeArray(ary) { var intialLength = ary.length; var aryNew = new Array(); var iCurrent; while (aryNew.length < intialLength) { iCurrent = randomNumberBetween(0, ary.length - 1); aryNew.push(ary[iCurrent]); ary.splice(iCurrent, 1); } return aryNew; } let ary = [1,1,2,2,3,3,4,4,5,5]; let set = new Set(ary); ary = Array.from(set); let randomAry = randomizeArray(ary) console.log(randomAry);

Instead of producing random numbers independently, create an array with all values between min and max, shuffle it, and then pick the number of values from it that you need:不是独立生成随机数,而是创建一个包含 min 和 max 之间的所有值的数组,对其进行混洗,然后从中选择您需要的值的数量:

So replace this:所以替换这个:

const rand = [];
for (var i = 0; i <= size - 1; i++) {
    rand.push(Math.floor(Math.random() * max) + min);
}

...with this: ...有了这个:

const rand = shuffle(Array.from({length: max - min + 1}, (_, i) => i + min).slice(0, size);

You can take the shuffle function from this answer :您可以从此答案中获取shuffle功能:

 function shuffle(a) { var j, x, i; for (i = a.length - 1; i > 0; i--) { j = Math.floor(Math.random() * (i + 1)); x = a[i]; a[i] = a[j]; a[j] = x; } return a; }

Be aware that you cannot slice more values than that are present in the min-to-max array.请注意,您不能分割比 min-to-max 数组中存在的值更多的值。 There is of course no way to take 10 unique integers from the range [4...6].当然没有办法从范围 [4...6] 中取出 10 个唯一整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM