如果我有一个类似于:[[a,b],[a,c],[b,a],[b,c],[c,a],[c,b]]的多维数组,那么如何遍历和删除重复序列,其中[a,b]和[b,a]是相同的。
而且,这个阵列实际上是巨大的,数以万计。必须向后执行for循环,因为每次迭代时数组长度都会缩小。我甚至不确定每个循环是否会对此起作用。我真的不知所措,只想知道如何开始。
而且,我试着搜索了大约一个小时,我甚至不知道如何表达它。
发布于 2015-01-01 22:21:07
我想我要尝试一种不同的方法来解决这个问题。我还认为它将比提出的一些解决方案更快(尽管我们当然需要测试它并对其进行基准测试)。
首先,我们为什么不利用面向哈希的javascript数组和对象的特性呢?我们可以创建一个包含关系的对象(为了创建一种映射),并将那些尚未存储的关系存储在一个新的数组中。使用这种方法,对象也没有问题,我们只需要为每个对象请求一个标识符或散列或其他任何东西。此标识符必须使它们之间的关系成为可能。
更新
守则:
var temp = {},
massive_arr = [['a','b'],['a','c'],['a','d'], ['b','a'],['b','c'],['b','d'],['c','a'],['c','b'],['c','d']],
final_arr = [],
i = 0,
id1,
id2;
for( ; i < massive_arr.length; i++ ) {
id0 = objectIdentifier(massive_arr[i][0]);// Identifier of first object
id1 = objectIdentifier(massive_arr[i][1]);// Identifier of second object
if(!temp[id0]) {// If the attribute doesn't exist in the temporary object, we create it.
temp[id0] = {};
temp[id0][id1] = 1;
} else {// if it exists, we add the new key.
temp[id0][id1] = 1;
}
if( id0 === id1 && !temp[id0][id1+"_bis"] ) {// Especial case [a,a]
temp[id0][id1+"_bis"] = 1;
final_arr.push(massive_arr[i]);
continue;// Jump to next iteration
}
if (!temp[id1]) {// Store element and mark it as stored.
temp[id1] = {};
temp[id1][id0] = 1;
final_arr.push(massive_arr[i]);
continue;// Jump to next iteration
}
if (!temp[id1][id0]) {// Store element and mark it as stored.
temp[id1][id0] = 1;
final_arr.push(massive_arr[i]);
}
}
console.log(final_arr);
function objectIdentifier(obj) {
return obj;// You must return a valid identifier for the object. For instance, obj.id or obj.hashMap... whatever that identifies it unequivocally.
}你可以测试它这里
第二次更新
虽然这并不是最初所要求的,但我已经对方法做了一些修改,以使其适应n长度的元素(如果需要,可以更改n)。
此方法速度较慢,因为它依赖排序为映射生成有效密钥。即使如此,我觉得它还是够快的。
var temp = {},
massive_arr = [
['a', 'a', 'a'], //0
['a', 'a', 'b'], //1
['a', 'b', 'a'],
['a', 'a', 'b'],
['a', 'c', 'b'], //2
['a', 'c', 'd'], //3
['b', 'b', 'c'], //4
['b', 'b', 'b'], //5
['b', 'b', 'b'],
['b', 'c', 'b'],
['b', 'c', 'd'], //6
['b', 'd', 'a'], //7
['c', 'd', 'b'],
['c', 'a', 'c'], //8
['c', 'c', 'a'],
['c', 'd', 'a', 'j'], // 9
['c', 'd', 'a', 'j', 'k'], // 10
['c', 'd', 'a', 'o'], //11
['c', 'd', 'a']
],
final_arr = [],
i = 0,
j,
ord,
key;
for (; i < massive_arr.length; i++) {
ord = [];
for (j = 0; j < massive_arr[i].length; j++) {
ord.push(objectIdentifier(massive_arr[i][j]));
}
ord.sort();
key = ord.toString();
if (!temp[key]) {
temp[key] = 1;
final_arr.push(massive_arr[i]);
}
}
console.log(final_arr);
function objectIdentifier(obj) {
return obj;
}它可以测试这里
发布于 2015-01-01 21:31:03
***
* Turns out the OP has objects in his list, so this approach won't
* work in that case. I'll leave this for future reference.
***
var foo = [['a','b'],['a','c'],['b','a'],['b','c'],['c','a'],['c','b']];
function removeRepeats(list) {
var i;
var b = [];
var _c = [];
for (i = 0; i < list.length; i++) {
var a = list[i].sort();
var stra = a.join("-");
if(_c.indexOf(stra) === -1) {
b.push(a);
_c.push(stra);
}
}
return b;
}
console.log(removeRepeats(foo));这不是我制作过的最漂亮的代码,但我想应该足够让您开始了。我要做的是创建两个新的数组,b和_c。b将是没有重复序列的数组。_c是一个助手数组,它包含已作为字符串处理的所有唯一对,因此我可以在遍历list时进行简单的字符串比较。
https://stackoverflow.com/questions/27734661
复制相似问题