猿问

用新数字替换重复项

我想在不改变数组长度的情况下从数组中删除重复项。如果有重复项,我想在该索引上生成一个新的随机数。然而,因为该号码可能已经存在,所以我需要重新生成它,直到它成为一个唯一的号码。


var arr = [5, 5, 5, 4, 4];

let counter = 0;

for (let i = 0; i < arr.Length; i++) {

  for (let j = i + 1; j < arr.Length; j++) {

    if (arr[i] === arr[j]) {

      arr[j] = Math.floor(Math.random() * 10);

      counter++;

    }

  }

  counter = 0;

}


牛魔王的故事
浏览 151回答 4
4回答

千巷猫影

使用 Set 或数组或对象来跟踪已经看到的值,并使用 while 循环来获取新的唯一值const arr = [5, 5, 5, 4, 4];const seen = new Set();// only holds unique values, even when add duplicatesarr.forEach((n,i) => {&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; while(seen.has(n)){&nbsp; &nbsp;arr[i] = n = Math.floor(Math.random() * 10);&nbsp; }&nbsp;&nbsp; seen.add(n);});console.log(arr)

慕工程0101907

previousElements 包含 arr 中位于当前索引之前的元素,而 otherElements 包含 arr 中除当前索引之外的所有其他元素。如果已经有一个实例,则会生成一个新的随机数。然后将新的随机数与 arr 的所有其余元素进行比较,以避免更改数字的第一个实例。在提供的示例中,索引 3 将始终保持为 4。var arr = [5, 5, 5, 4, 4];for (let i = 0; i < arr.length; i++) {&nbsp; &nbsp; let previousElements = arr.slice(0, i);&nbsp; &nbsp; let otherElements = JSON.parse(JSON.stringify(arr));&nbsp; &nbsp; otherElements.splice(3, 1);&nbsp; &nbsp; if (previousElements.includes(arr[i])) {&nbsp; &nbsp; &nbsp; &nbsp; arr[i] = Math.floor(Math.random() * 10);&nbsp; &nbsp; &nbsp; &nbsp; while (otherElements.includes(arr[i])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[i] = Math.floor(Math.random() * 10);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}console.log('arr: ' + JSON.stringify(arr));示例输出:[5,8,2,4,3]示例输出:[5,0,1,4,8]

海绵宝宝撒

这是解决方案:var arr = [5, 5, 5, 4, 4];const replaceduplicates=(arr)=>{&nbsp; &nbsp; let i =1;&nbsp; &nbsp; &nbsp;while(i < arr.length -1 ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const currentElement=arr[i]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const indexOfcurrentElement= arr.indexOf(currentElement)&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(indexOfcurrentElement > -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;let newValue=Math.floor(Math.random() * 10 + arr[i-1])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while( arr.indexOf(newValue) > -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;newValue=Math.floor(Math.random() * 10 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr[i] = newValue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;return arr}//this is how I tested basically this will run for ever&nbsp;let found=falsewhile(!found ){&nbsp; &nbsp; const arrResponse =replaceduplicates(arr)&nbsp; &nbsp; for (let i = 0; i < arrResponse.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (let j = i+1; j < arrResponse.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(arrResponse[i] == arrResponse[j]) found = true&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; console.log('no duplicates')}

不负相思意

使用while循环而不是if语句来不断检查该值,直到它不等于先前的值。虽然您需要在将数字更改为随机数之后添加额外的检查,以查看该随机数是否已经不在数组中。const hasDuplicateNumbers = (number, numbers) =>&nbsp;&nbsp; numbers.filter(item => item === number).length > 1;let arr = [5, 5, 5, 4, 4];for (let i = 0; i < arr.length; i++) {&nbsp; for (let j = i + 1; j < arr.length; j++) {&nbsp; &nbsp; if (i === j) {&nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; }&nbsp; &nbsp; while(hasDuplicateNumbers(arr[j], arr)) {&nbsp; &nbsp; &nbsp; arr[j] = Math.floor(Math.random() * 10);&nbsp; &nbsp; }&nbsp; }}console.log(arr);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答