English 中文(简体)
flagging duplicates in an array
原标题:

After requesting some user pictures, I end up with an array like this:

[
    { label: "portrait", owner: "Jon" },
    { label: "house", owner: "Jim" },
    { label: "portrait", owner: "Jim" },
    { label: "portrait", owner: "Jane" },
    { label: "cat", owner: "Jane" }
]

I need to highlight items whose label occurs multiple times in the array (here: "portrait").

Ideally, there d be one flag (A) for items whose label occurs again later in the array, and another flag (B) for the final occurrence (i.e. both Jon s and Jim s portraits are flagged A, while Jane s gets B).

Any help would be greatly appreciated!

最佳回答

I m not sure what you re trying to do, but maybe you just need to aggregate the data?

var data = [
    { name: "portrait", owner: "Jon" },
    { name: "house", owner: "Jim" },
    { name: "portrait", owner: "Jim" },
    { name: "portrait", owner: "Jane" },
    { name: "cat", owner: "Jane" }
];

var byName = {};

for (var i = 0, l = data.length; i < l; ++i) {
    if (!byName[data[i].name]){
        byName[data[i].name] = [];
    }
    byName[data[i].name].push(data[i].owner);
}

// byName == {portrait: ["Jon", "Jim", "Jane"], house: ["Jim"], cat: ["Jane"]}

var byOwner = {};

for (var i = 0, l = data.length; i < l; ++i) {
    if (!byOwner[data[i].owner]) {
        byOwner[data[i].owner] = [];
    }
    byOwner[data[i].owner].push(data[i].name);
}

Or maybe you like this better:

var data = [
    { name: "portrait", owner: "Jon" },
    { name: "house", owner: "Jim" },
    { name: "portrait", owner: "Jim" },
    { name: "portrait", owner: "Jane" },
    { name: "cat", owner: "Jane" }
];

var byName = [];

for (var i = 0, l = data.length; i < l; ++i) {
    var done = false;
    for (var j = 0, k = data.length; j < k; ++j) {
        if (byName[j] && byName[j].name == data[i].name) {
            byName[j].data.push(data[i].owner);
            done = true;
        }
    }
    if (!done) {
        byName.push({name: data[i].name, data: [data[i].owner]});
    }
}

/*
byName == [
    {name: "portrait", data: ["Jon", "Jim", "Jane"]},
    {name: "house", data: ["Jim"]},
    {name: "cat", data: ["Jane"]}
] */

Because posting random code is fun and you know it!

问题回答

I d suggest looping through the picture array and adding a flagged member to each object, depending if it s duplicate. See below for an example. It will assign anything with a duplicate with an A and the last duplicate with a B . Anything that is not a duplicate is not flagged.

var picture_array = [
        {label:  portrait , owner: "Jon"},
        {label:  house , owner: "Jim"},
        {label:  portrait , owner: "Jim"},
        {label:  portrait , owner: "Jane"},
        {label:  cat , owner: "Jane"}
    ],
    length = picture_array.length;

//Loop through picture_array

for(var i = 0; i < length; i++) {
    var picture = picture_array[i],
        label = picture.label;

    //If picture has already been flagged, go the the next picture
    if (picture.flagged) {
       continue;
    }

    //Loop through rest of the array to compare duplicate labels
    var picture_a = picture;
    for(var j = i + 1; j < length; j++) {
        var picture_b = picture_array[j];

        //If picture_a matches picture_b then flag both of them appropriately
        if (picture_a.label == picture_b.label) {
            picture_a.flagged =  A ;
            picture_b.flagged =  B ;
            picture_a = picture_b;
        }
    }
}




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签