在对数组进行排序时删除重复项

Remove duplicates while sorting an array

本文关键字:删除 排序 数组      更新时间:2023-09-26

我有一个数组,其中包含我需要排序的对象,并根据每个数组项的 3 个特定值删除重复项。目前我正在使用两个循环(非嵌套(:一个用于排序,另一个用于删除项目。有没有办法在排序或类似的东西时删除重复项?对于 1000 多个项目,以下代码非常慢,我想知道是否有更快的方法:

var list = [
    {a: "Somename", b: "b", c: 10},
    {a: "Anothername", b: "a", c: 12},
    // and so on
];
function sortList(list) {
    // Sort the list based on the 3 specific values
    list = list.sort(function(a,b) {
        a.a = a.a.toLowerCase();
        b.a = b.a.toLowerCase();
        if (a.a < b.a) return -1;
        if (a.a > b.a) return 1;
        a.b = a.b.toLowerCase();
        b.b = b.b.toLowerCase();
        if (a.b < b.b) return -1;
        if (a.b > b.b) return 1;
        if (a.c < b.c) return -1;
        if (a.c > b.c) return 1;
        return 0;
    });
    // Loop through removing duplicates
    for (var a,b,i=list.length-1; i > 0; i--) {
        a = list[i];
        a.a = a.a.toLowerCase();
        a.b = a.b.toLowerCase();
        b = list[i-1];
        b.a = b.a.toLowerCase();
        b.b = b.b.toLowerCase();
        if (a.a===b.a && a.b===b.b && a.c===b.c) list.splice(i-1,1);
    }
    return list;
}
list = sortList(list);

请不要使用jQuery答案,或建议使用其他库的答案。导入库来做这么简单的事情似乎有点矫枉过正。

不要在第二个循环中使用splice,只需将唯一值放在新数组中即可。因此,在每次迭代中,您只需将值附加到新数组,而不是splice现有数组; pushsplice快得多.例如:

var newList = [];
if (list) {
    newList.push(list[0];
    for (var a,b,i=1; i < list.length; i++) {
        a = list[i];
        a.a = a.a.toLowerCase();
        a.b = a.b.toLowerCase();
        b = list[i-1];
        b.a = b.a.toLowerCase();
        b.b = b.b.toLowerCase();
        if (a.a!==b.a || a.b!==b.b && a.c!==b.c) {
            newList.push(a);
        }
    }
}

在搜索了周围之后,我想出了自己的答案。它仍然使用这两个循环,但这比排序然后删除重复项要快得多,并且比在排序时跟踪重复项更一致。基本上,我遍历数组,使用我想要排序/删除重复项的属性值('key'(的串联将每个项目保存在一个对象中。如果密钥已经存储,我知道它是重复的,可以继续下一个。否则,我将项目添加到新数组中。删除重复项后,我对数组进行排序并返回:

function sortList(list){
    // Since the 3 properties I'm sorting by can be converted to string I can loop
    // through the specified array, creating 'keys' by concating those property values
    // and adding those 'keys' to a temporary object. If the 'key' is present in the
    // temporary object, I know it's a duplicate in the array and can ignore it.
    // Otherwise I know it's not a duplicate and add it to a new array
    for (var a=0,b=list.length,item,key="",dupObj={},nList=[]; a<b; a++) {
        item=list[a];
        key=item.a.toLowerCase()+"_"+item.b.toLowerCase()+"_"+item.c;
        if(!dupObj[key]) {
            dupObj[key]=true;
            nList.push(item);
        }
    }
    // Now that we have an array without duplicates we can sort the array:
    nList.sort(function(a,b) {
        if ((a.a = a.a.toLowerCase()) < (b.a = b.a.toLowerCase()) return -1;
        if (a.a > b.a) return 1;
        if ((a.b = a.b.toLowerCase()) < (b.b = b.b.toLowerCase())) return -1;
        if (a.b > b.b) return 1;
        if (a.c < b.c) return -1;
        if (a.c > b.c) return 1;
        return 0;
    });
    // return the new list
    return nList;
}
var list = [
    {a: "Somename", b: "b", c: 10},
    {a: "Anothername", b: "a", c: 12},
    // and so on
];
list = sortList(list);