I'm trying to figure out how to compare if certain elements in two arrays compare in the same order.
我想知道如何比较两个数组中的某些元素是否以相同的顺序进行比较。
var compare = function (arr1, arr2) {
//........
}
compare ([f,t,r,m], [s,f,t,r,q,p,m]); //should return true
compare ([f,t,r,m], [f,a,t,,m,r]); //should return false
I proceeded with a for loop and then identifying when the values match, then I'm pretty sure you should compare the arrays but I feel I'm missing something.
我继续执行for循环,然后确定值何时匹配,然后我很确定应该比较数组,但我觉得我漏掉了一些东西。
var compare = function (a, b) {
a.forEach(function(letter){
for (i=0; i<b.length; i++) {
if (letter===b[i]) {}
}
})
}
4 个解决方案
#1
2
Both of these functions will do this comparison with a O(n)
runtime, where Ori Drori's solution runs in O(n^2)
这两个函数将与O(n)运行时,Ori德的解决方案在O(n ^ 2)
var a = [1,2,3];
var b = [0,1,4,3,9,10,2,5,6]; // 1,2,3 in wrong order
var c = [0,4,1,5,6,2,8,3,5]; // 1,2,3 in right order
// using foreach
function compare(a,b){
var i = 0;
b.forEach(function(el){
if(el == a[i]) i++;
})
return i == a.length;
}
// using reduce
function compare2(a,b){
return b.reduce(function(i, el){
return el == a[i] ? i + 1 : i;
}, 0) == a.length;
}
console.log(compare(a,b) == false); // should be false
console.log(compare(a,c) == true); // should be true
console.log(compare2(a,b) == false); // should be false
console.log(compare2(a,c) == true); // should be true
#2
2
Remove all letters from the 2nd array that don't appear in 1st array using Array#filter and Array#indexOf. Then iterate the result with Array#every, and check if every character appears in the same place in the 1st array:
使用数组#filter和数组#indexOf从第一个数组中删除第2个数组中没有出现的所有字母。然后使用数组#every迭代结果,并检查每个字符是否出现在第一个数组的相同位置:
function compare(a, b) {
var arr = b.filter(function(c) {
return a.indexOf(c) !== -1; // use a hash object instead of indexOf if the arrays are large
});
return arr.every(function(c, i) {
return c === a[i];
});
}
console.log(compare(['f','t','r','m'], ['s','f','t','r','q','p','m'])); //should return true
console.log(compare(['f','t','r','m'], ['f','a','t','m','r'])); //should return false
#3
2
You could take an index for array2
and check while iterating and return the comparison of the index and array2
and the element of array1
.
您可以获取array2的索引并在迭代时进行检查,并返回索引和array2以及array1的元素的比较。
function compare(array1, array2) {
var i = 0;
return array1.every(function (a) {
while (i < array2.length && a !== array2[i]) {
i++;
}
return a === array2[i++];
});
}
console.log(compare(['f', 't', 'r', 'm'], ['s', 'f', 't', 'r', 'q', 'p', 'm'])); // true
console.log(compare(['f', 't', 'r', 'm'], ['f', 'a', 't', , 'm', 'r'])); // false
#4
0
You may do as follows;
你可以这样做;
function compare(a,b){
return b.filter(e => a.includes(e))
.every((e,i) => e === a[i])
}
console.log(compare(["f","t","r","m"], ["s","f","t","r","q","p","m"]));
console.log(compare(["f","t","r","m"], ["f","a","t","m","r"]));
#1
2
Both of these functions will do this comparison with a O(n)
runtime, where Ori Drori's solution runs in O(n^2)
这两个函数将与O(n)运行时,Ori德的解决方案在O(n ^ 2)
var a = [1,2,3];
var b = [0,1,4,3,9,10,2,5,6]; // 1,2,3 in wrong order
var c = [0,4,1,5,6,2,8,3,5]; // 1,2,3 in right order
// using foreach
function compare(a,b){
var i = 0;
b.forEach(function(el){
if(el == a[i]) i++;
})
return i == a.length;
}
// using reduce
function compare2(a,b){
return b.reduce(function(i, el){
return el == a[i] ? i + 1 : i;
}, 0) == a.length;
}
console.log(compare(a,b) == false); // should be false
console.log(compare(a,c) == true); // should be true
console.log(compare2(a,b) == false); // should be false
console.log(compare2(a,c) == true); // should be true
#2
2
Remove all letters from the 2nd array that don't appear in 1st array using Array#filter and Array#indexOf. Then iterate the result with Array#every, and check if every character appears in the same place in the 1st array:
使用数组#filter和数组#indexOf从第一个数组中删除第2个数组中没有出现的所有字母。然后使用数组#every迭代结果,并检查每个字符是否出现在第一个数组的相同位置:
function compare(a, b) {
var arr = b.filter(function(c) {
return a.indexOf(c) !== -1; // use a hash object instead of indexOf if the arrays are large
});
return arr.every(function(c, i) {
return c === a[i];
});
}
console.log(compare(['f','t','r','m'], ['s','f','t','r','q','p','m'])); //should return true
console.log(compare(['f','t','r','m'], ['f','a','t','m','r'])); //should return false
#3
2
You could take an index for array2
and check while iterating and return the comparison of the index and array2
and the element of array1
.
您可以获取array2的索引并在迭代时进行检查,并返回索引和array2以及array1的元素的比较。
function compare(array1, array2) {
var i = 0;
return array1.every(function (a) {
while (i < array2.length && a !== array2[i]) {
i++;
}
return a === array2[i++];
});
}
console.log(compare(['f', 't', 'r', 'm'], ['s', 'f', 't', 'r', 'q', 'p', 'm'])); // true
console.log(compare(['f', 't', 'r', 'm'], ['f', 'a', 't', , 'm', 'r'])); // false
#4
0
You may do as follows;
你可以这样做;
function compare(a,b){
return b.filter(e => a.includes(e))
.every((e,i) => e === a[i])
}
console.log(compare(["f","t","r","m"], ["s","f","t","r","q","p","m"]));
console.log(compare(["f","t","r","m"], ["f","a","t","m","r"]));