我想按升序对字符串数组进行排序,而不会遗漏序列,并判断是否有重复项

到目前为止,这就是我的职能

const sortAndCheckSequence = async (value) => {
let data = [...value]; // suppose value is ['1','2','1.1','3','1.1.1','1.2','3.1']

sortedData = data.sort((a, b) => (a > b ? 1 : -1));

// sortedData ['1','1.1','1.1.1','1.2','2','3','3.1']

const CheckSequence = (sortedData) =>{

// check sequence return true if there is no missing sequence and no duplicates else return false
  }
 }

// assuming data is sorted
sortAndCheckSequence(['1','1.1','1.1.1','1.2','2','3','3.1']) // true
sortAndCheckSequence(['1','1.1','1.1.1','1.2','2','2.1','3','4','5']) // true
sortAndCheckSequence(['1','1.1','1.1.1','1.2','2','2.1','3','4','4.1','5','5.1.1']) // false '5.1' missing 
sortAndCheckSequence(['1','1.1','1.1.1','1.2','2','2.1','3','4','5','1.2']) // false '1.2' is duplicate
sortAndCheckSequence(['1','1.1','1.1.1','1.2','1.4','2','3','3.1']) // false '1.3' missing
sortAndCheckSequence(['1','1.1','1.1.1','1.1.2','1.2','1.4','2','2.1','9','3','3.1']) //false '9' is not in sequence 

推荐答案

给定值x.y.z,后面只有四个可能的值:

  • x.y.z.1
  • x.y.(z+1)
  • x.(y+1)
  • (x+1)

如果我们通过删除每个值与前一个值相同的任何前缀来判断每个值,事情就会变得更容易:

  • x.y.z.1个 truncates to .1 (versus "" – the entire previous value is the common prefix)
  • x.y.(z+1)个 truncates to (z+1) (versus z)
  • x.(y+1)个 truncates to (y+1) (versus y.z)
  • (x+1)个 has no common prefix, so remains (x+1)个 versus x.y.z

因此,我们只需要判断每个值是不是前一个值附加了.1,或者它是否有一个共同的前缀,下一个数字递增.

对于奖励积分,我们可以单独检测重复项,尽管不清楚您是否对精确的验证消息感兴趣.

const sortAndCheckSequence = (value) => {
  let data = [...value];

  sortedData = data.sort((a, b) => (a > b ? 1 : -1));

  const removeCommonPrefix = (a, b) => {
    const minLen = Math.min(a.length, b.length);

    let i = 0;
    while (i < minLen && a[i] === b[i]) ++i;

    return [a.substr(0, i), a.substr(i), b.substr(i)];
  }

  const FindError = (sortedData) => {
    for (let i = 1; i < sortedData.length; ++i) {
      const [prevValue, value] = [sortedData[i - 1], sortedData[i]];

      if (value === prevValue)
        return `Duplicate ${value}`;

      const [prefix, prevSuffix, suffix] = removeCommonPrefix(prevValue, value);

      if (prevSuffix === "") {
        if (suffix !== ".1")
          return `Expected ${prevValue}.1, got ${value}`;
      } else if (suffix !== String(parseInt(prevSuffix) + 1)) {
        return `Expected ${prefix}${parseInt(prevSuffix) + 1}, got ${value}`;
      }
    }
    return null; // no error
  }

  const error = FindError(sortedData);
  console.log(error || "No error");

  return error === null; // Does the sequence pass the check without any errors?
}

sortAndCheckSequence(['1', '1.1', '1.1.1', '1.2', '2', '3', '3.1']); // true
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.2', '2', '2.1', '3', '4', '5']); // true
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.1.1.1', '1.1.1.2', '1.2', '3', '3.1']); // false: 2 is missing
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.2', '2', '2.1', '3', '4', '4.1', '5', '5.1.1']); // false '5.1' missing 
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.2', '2', '2.1', '3', '4', '5', '1.2']); // false '1.2' is duplicate
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.2', '1.4', '2', '3', '3.1']); // false '1.3' missing
sortAndCheckSequence(['1', '1.1', '1.1.1', '1.1.2', '1.2', '1.3', '1.4', '2', '2.1', '9', '3', '3.1']); //false '9' is not in sequence

Javascript相关问答推荐

在JavaScript中,如何将请求的回调函数的结果合并到运行的代码中?

JQuery. show()工作,但. hide()不工作

浮动Div的淡出模糊效果

WebGL 2.0无符号整数输入变量

CheckBox作为Vue3中的一个组件

在浏览器中触发插入事件时检索编码值的能力

Vaadin定制组件-保持对javascrip变量的访问

try 使用PM2在AWS ubuntu服务器上运行 node 进程时出错

AJAX POST在控制器中返回空(ASP.NET MVC)

如果没有页面重新加载Angular ,innerHTML属性绑定不会更新

JS Animate()方法未按预期工作

Docent.cloneNode(TRUE)不克隆用户输入

输入的值的类型脚本array.排序()

使用Reaction窗体挂钩注册日历组件

如何在AG-Grid文本字段中创建占位符

为什么当雪碧的S在另一个函数中时,Phaser不能加载它?

为什么这个最小Angular 的Licial.dev设置不起作用?

在传单的图像覆盖中重新着色特定 colored颜色 的所有像素

验证Java脚本函数中的两个变量

JavaScript -如何跳过某个字符(S)来打乱字符串中的字符