javascript - 为什么我的 for 循环重复我的值?

标签 javascript

我有一个函数可以找到 3 个独特点的组合。

在其他语言中,例如 java,相同的代码可以正常工作。

但是,在 javascript 中,它会重复输出量。

function findCombinations(lines) {
  let count = 0;
  let points = [];
  let res = [];
  for (let line of lines) {
    let pointArray = (line).split('');
    for (let c of pointArray) {
      points.push(c);
    }
  }
  points.sort();
  points.reverse();
  points = points.filter(function(elem, index, self) {
    return index === self.indexOf(elem);
  });
  console.log("Unique points " + points);

  for (let i = 0; i < points.length - 2; i++) {
    for (let j = i + 1; j < points.length - 1; j++) {
      for (let k = j + 1; k < points.length; k++) {
        let combination = points[i] + points[j] + points[k];

        let p1 = false;
        let p2 = false;
        let p3 = false;

        for (let line of lines) {

          if (line.includes(points[i]) && line.includes(points[j]) && !line.includes(points[k])) {
            p1 = true;
          }

          if (line.includes(points[i]) && line.includes(points[k]) && !line.includes(points[j])) {
            p2 = true;
          }

          if (line.includes(points[j]) && line.includes(points[k]) && !line.includes(points[i])) {
            p3 = true;
          }

          if (p1 && p2 && p3) {
            count++;
            res.push(combination);
            console.log('Found it: ' + count + ' Combination: ' + combination);
          }
        }
      }
    }
  }

  console.log("Total Triangles: " + res.length + ' ' + res);
}

findCombinations(["AKLM", "BDNOPQR", "EWVUTS", "21IZ", "CJ",
  "ABC", "KNWF2", "LPUXH1", "MRSYZJ",
  "ADEFGHIJ", "MQTXG2C", "KOVXY"
])

我发现清除重复项的唯一方法是使用以下方法过滤结果数组:

  res = res.filter(function(elem, index, self) {
    return index === self.indexOf(elem);
  });

我不喜欢这样,我想知道为什么它会重复以及如何避免它?

最佳答案

这段代码:

          if (p1 && p2 && p3) {
            count++;
            res.push(combination);
            console.log('Found it: ' + count + ' Combination: ' + combination);
          }

不应位于for(let line oflines)循环内。您将插入组合并为每条匹配的行增加计数器,而不是在有任何匹配时增加一次。

function findCombinations(lines) {
  let count = 0;
  let points = [];
  let res = [];
  for (let line of lines) {
    let pointArray = (line).split('');
    for (let c of pointArray) {
      points.push(c);
    }
  }
  points.sort();
  points.reverse();
  points = points.filter(function(elem, index, self) {
    return index === self.indexOf(elem);
  });
  console.log("Unique points " + points);

  for (let i = 0; i < points.length - 2; i++) {
    for (let j = i + 1; j < points.length - 1; j++) {
      for (let k = j + 1; k < points.length; k++) {
        let combination = points[i] + points[j] + points[k];

        let p1 = false;
        let p2 = false;
        let p3 = false;

        for (let line of lines) {

          if (line.includes(points[i]) && line.includes(points[j]) && !line.includes(points[k])) {
            p1 = true;
          }

          if (line.includes(points[i]) && line.includes(points[k]) && !line.includes(points[j])) {
            p2 = true;
          }

          if (line.includes(points[j]) && line.includes(points[k]) && !line.includes(points[i])) {
            p3 = true;
          }

        }
        if (p1 && p2 && p3) {
          count++;
          res.push(combination);
          console.log('Found it: ' + count + ' Combination: ' + combination);
        }
      }
    }
  }

  console.log("Total Triangles: " + res.length + ' ' + res);
}

findCombinations(["AKLM", "BDNOPQR", "EWVUTS", "21IZ", "CJ",
  "ABC", "KNWF2", "LPUXH1", "MRSYZJ",
  "ADEFGHIJ", "MQTXG2C", "KOVXY"
])

关于javascript - 为什么我的 for 循环重复我的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58566182/

相关文章:

javascript - 如何在 chart.js 中将对数刻度分配给 y 轴?

javascript - 双列布局,两列高度相同且都填满屏幕的全高

javascript - 类型错误 : Cannot find function getHours in object 17

javascript - 来自 codepen 的 JS 在除 codepen 之外的任何地方都不起作用

javascript - 连接/拼接 Web 图像资源以缩短加载时间

JavaScript:将字符串 push() 到数组中返回 **null**

c# - 当在内容上方包含 javascript 时,HTML 页面消失

javascript - Kendo Grid 中的寻呼机错误(Nan-Nan of 1 items)

JavaScript 使 AJAX 内容淡入

javascript - 如何将一个数组中的连续值插入到另一个映射数组中?