按常用值合并数组

时间:2013-09-09 15:54:01

标签: javascript jquery

我真的无法理解这一点。

我该怎么做:

varAllMix = [[1,2], [3,4], [1,4], [5,6], [7,2], [8,5], [9,10], [11,6]];

对此:

varComGroup = [[1,2,3,4,7],[5,6,8,11],[9,10]]

例如:[1,2,3,4,7]是由[1,2], [3,4], [1,4], [7,2]合并公共值产生的:

[1,2] + [1,4] -> [1, 2, 4]
[1, 2, 4] + [3,4] -> [1, 2, 3, 4]
[1, 2, 3, 4] + [7, 2] -> [1, 2, 3, 4, 7]

2 个答案:

答案 0 :(得分:1)

这样做非常有效(并且每个allMix元组甚至支持两个以上的值):

var AllMix = [[1,2], [3,4], [1,4], [5,6], [7,2], [8,5], [9,10], [11,6]];

var ComGroup = [],
    lookup = {}; // a lookup table for sets in ComGroup per value
for (var i=0; i<AllMix.length; i++) {
    var sets = [], // sets in ComGroup that contain one of the tuple values
        add = []; // new, yet unknown values
    for (var j=0, l=AllMix[i].length; j<l; j++) {
        var val = AllMix[i][j];
        if (val in lookup) {
            if (sets.indexOf(lookup[val]) == -1)
                sets.push(lookup[val]);
        } else
            add.push(val);
    }
    var merge = sets.shift(), // the set to merge into (or add values)
        addFrom; // the number of values in merge that are known to lookup
    if (merge) {
        addFrom = merge.length;
        if (add.length) sets.push(add); // add the new values to the merged sets
    } else { // no set was found in lookup that contains numbers from the tuple
        ComGroup.push(merge = add); // make the new numbers the new set
        addFrom = 0;
    }
    for (var j=0; j<sets.length; j++) // merge all sets into merge
        merge.push.apply(merge, sets[j].splice(0)); // and empty them
    for (var l=merge.length; addFrom<l; addFrom++) // for all new numbers
        lookup[merge[addFrom]] = merge; // update the set lookup table
}
ComGroup = ComGroup.filter(function(set) {
    // sort each set numerically and remove the empty ones
    return set.sort(function(a,b){return a-b;}).length;
});

答案 1 :(得分:0)

您可以通过添加对

来逐步构建结果
m = {}; // map from an element to the connected component
result = [];

for (var i=0; i < lists.lenght; i++) {
    var a = lists[i][0], b = lists[i][1];
    var ca = m[a];
    var cb = m[b];
    if (ca && cb) {
        if (ca !== cb) {
            // two connected components got merged
            for (var bi=0; bi < cb.lenght; bi++) {
                ca.push(cb[bi]);
                m[cb[bi]] = ca;
            }
            cb.splice();
        }
    } else if (ca) {
        ca.push(b); // extending connected component of a
    } else if (cb) {
        cb.push(a); // extending connected component of b
    } else {
        // create a new connected component
        result.push(m[a] = m[b] = [a, b]);
    }
}

// remove leftovers from merge operations
result = result.filter(function(x){return x.length;});