我有一个函数可以找到 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/