【问题标题】:Find the duplicate string in the array and add an incrementing number at the end在数组中找到重复的字符串,并在末尾添加一个递增的数字
【发布时间】:2021-12-30 05:32:07
【问题描述】:

我正在使用 javascript 并试图找出一种算法,根据程序中的现有字符串将增量数字附加到字符串。

输入:

['Untitled Form', 'Untitled Form - 1', 'Untitled Form - 5', 'Untitled Form - 3', 'Untitled Form - 4', "Untitled Form"];

假设用户想在这个列表中添加另一个字符串,并且他们选择了“Untitled Form”作为要添加的字符串。因此,给定输入和现有列表,算法将在列表中搜索以检查是否缺少任何递增数字,例如在上面的代码中,“无标题表格 - 2”丢失,它应该返回“无标题表格 - 2”为要添加的新字符串。

输出:

['Untitled Form', 'Untitled Form - 1', 'Untitled Form - 5', 'Untitled Form - 3', 'Untitled Form - 4', "Untitled Form - 2"];

这与在 Windows 资源管理器中不断添加新文件夹的功能相同(“新文件夹 (1)”、“新文件夹 (2)”等等)

我有这个问题的解决方案,但我认为它不是很有效,并且该算法还会检查最高数字并在“无标题表格 - 6”之后添加增量数字。

【问题讨论】:

    标签: javascript arrays reactjs angular sorting


    【解决方案1】:

    您可以通过提取现有的indexes并对其进行排序,然后将每个索引与下一个索引进行比较,如果差异大于1,那么您可以意识到那个地方有一个丢失的索引。像这样:

    let list = ['Untitled Form', 'Untitled Form - 1', 'Untitled Form - 5', 'Untitled Form - 3', 'Untitled Form - 4'];
    
    const findNextIndex = (arr)=> {
        const sortedArr = arr.map(item => Number(item.split('-')[1] || 0)).sort((a,b)=> a-b);
        if(!sortedArr.includes(0)) return 0;
        let index = sortedArr[sortedArr.length-1] + 1; //maximum value
        for(let i =0; i< sortedArr.length; i++){
          if(sortedArr[i+1] - sortedArr[i] > 1){
            index = sortedArr[i] + 1;
            break;
          }
        }
        return index;
    }
    
    const nextIndex = findNextIndex(list);
    list = [...list, `Untitled Form${nextIndex ? ` - ${nextIndex}` : ''}`]
    console.log(list)

    【讨论】:

    • 不适用于此列表 [“无标题表格 - 6”、“无标题表格 - 15”、“无标题表格 - 4”、“无标题表格 - 8”、“无标题表格 - 3” ,“无标题表格 - 7”,“无标题表格 - 5”]。它应该返回“Untitled Form”而不是返回“Untitled Form - 9”。
    • 我已经编辑了答案。
    • 现在可以了,谢谢!!
    • 不客气。
    【解决方案2】:

    这应该可以工作

    1. 对数组进行排序,使其变为
        [
            [0] => 'Untitled Form',
            [1] => 'Untitled Form - 1',
            [2] => 'Untitled Form - 2',
            ....
        ]
    
    1. 循环遍历,发现indexInTitle 与数组的索引不匹配的findIndex。

    2. 如果找到,则返回该索引,否则返回增量索引,

    const listMissing2 = ['Untitled Form', 'Untitled Form - 1', 'Untitled Form - 5', 'Untitled Form - 3', 'Untitled Form - 4'];
    const listNotMissing = ['Untitled Form', 'Untitled Form - 1', 'Untitled Form - 5', 'Untitled Form - 3', 'Untitled Form - 4', 'Untitled Form - 2'];
    
    const findMissingOrIncrementIndex = list => {
      const sortedList = list.sort();
      const missingIndex = sortedList.findIndex((title, index) => {
        const [, indexInTitle] = title.split(' - ');
        return index > 0 && Number(indexInTitle) !== index;
      });
      return (missingIndex > -1) ? missingIndex : list.length;
    }
    
    console.log(findMissingOrIncrementIndex(listMissing2));
    console.log(findMissingOrIncrementIndex(listNotMissing));

    【讨论】:

      猜你喜欢
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-02
      相关资源
      最近更新 更多