【问题标题】:Replace duplicates with new numbers用新号码替换重复项
【发布时间】:2021-02-08 04:21:30
【问题描述】:

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

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;
}

【问题讨论】:

  • 创建一个数字数组,然后随机播放,然后 pop() 唯一的随机数来填补你的漏洞。
  • 这能回答你的问题吗? Create an array with random values

标签: javascript arrays duplicates


【解决方案1】:

使用 Set 或数组或对象来跟踪已经看到的值,并使用 while 循环来获取新的唯一值

const arr = [5, 5, 5, 4, 4];
const seen = new Set();// only holds unique values, even when add duplicates

arr.forEach((n,i) => {      
  while(seen.has(n)){
   arr[i] = n = Math.floor(Math.random() * 10);
  } 
  seen.add(n);
});

console.log(arr)

【讨论】:

    【解决方案2】:

    这就是解决方案:

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

    【讨论】:

      【解决方案3】:

      previousElements 包含 arr 中当前索引之前的元素,而 otherElements 包含除当前索引之外的所有其他 arr 元素。如果已经存在实例,则会生成一个新的随机数。然后将新的随机数与 arr 的所有其余元素进行比较,以避免更改数字的第一个实例。在提供的示例中,索引 3 将始终保持为 4。

      var arr = [5, 5, 5, 4, 4];
      
      for (let i = 0; i < arr.length; i++) {
          let previousElements = arr.slice(0, i);
          let otherElements = JSON.parse(JSON.stringify(arr));
          otherElements.splice(3, 1);
      
          if (previousElements.includes(arr[i])) {
              arr[i] = Math.floor(Math.random() * 10);
      
              while (otherElements.includes(arr[i])) {
                  arr[i] = Math.floor(Math.random() * 10);
              }
          }
      }
      
      console.log('arr: ' + JSON.stringify(arr));
      

      样本输出:[5,8,2,4,3]

      样本输出:[5,0,1,4,8]

      【讨论】:

        【解决方案4】:

        使用while 循环而不是if 语句来不断检查该值,直到它不等于之前的值。

        虽然您需要在将数字更改为随机数之后添加额外的检查,以查看该随机数是否不在数组中。

        const hasDuplicateNumbers = (number, numbers) => 
          numbers.filter(item => item === number).length > 1;
        
        let arr = [5, 5, 5, 4, 4];
        for (let i = 0; i < arr.length; i++) {
          for (let j = i + 1; j < arr.length; j++) {
            if (i === j) {
              continue;
            }
            while(hasDuplicateNumbers(arr[j], arr)) {
              arr[j] = Math.floor(Math.random() * 10);
            }
          }
        }
        
        console.log(arr);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-03
          • 1970-01-01
          • 2019-04-29
          相关资源
          最近更新 更多