最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove duplicate element pairs from multidimensional array - Stack Overflow

programmeradmin2浏览0评论

I have an array that looks like this:

1.  coordinates = [ [16.343345, 35.123523],
2.                  [14.325423, 34.632723],
3.                  [15.231512, 35.426914],
4.                  [16.343345, 35.123523],
5.                  [15.231512, 32.426914] ]

The latitude on line 5 is the same as on line 3, but they have different longitudes and are therefore not duplicates.

Both the latitude and longitude are the same on line 3 and 6, and are therefore duplicates and one should be removed.

I have an array that looks like this:

1.  coordinates = [ [16.343345, 35.123523],
2.                  [14.325423, 34.632723],
3.                  [15.231512, 35.426914],
4.                  [16.343345, 35.123523],
5.                  [15.231512, 32.426914] ]

The latitude on line 5 is the same as on line 3, but they have different longitudes and are therefore not duplicates.

Both the latitude and longitude are the same on line 3 and 6, and are therefore duplicates and one should be removed.

Share Improve this question edited Jan 19, 2013 at 16:22 holyredbeard asked Jan 19, 2013 at 15:23 holyredbeardholyredbeard 21.2k32 gold badges110 silver badges174 bronze badges 5
  • 3 You should update your question to include any attempts you've made already. – Matt Commented Jan 19, 2013 at 15:25
  • 2 Have you tried sorting them and then looping over the result comparing the current record to the next? – Dave Commented Jan 19, 2013 at 15:26
  • 1 15,231512, 35,426914 (4 integers) shouldn't it be 15.231512, 35.426914 (2 floats)? – Fabrício Matté Commented Jan 19, 2013 at 15:36
  • For tasks like removing duplicates from lists, your best bet is often to use one of the pre-existing JS libraries like lodash, since this code has a lot more road-testing in it than any code you might write. – Dancrumb Commented Jan 19, 2013 at 16:29
  • 1 Can you clarify your question, also? Line 6 doesn't exist and, by my reckoning, lines 1 and 4 & lines 3 and 5 match – Dancrumb Commented Jan 19, 2013 at 16:30
Add a comment  | 

7 Answers 7

Reset to default 13

The difficulty in this question that different arrays never compare equal even if they contain same values. Therefore direct comparison methods, like indexOf won't work.

The following pattern might be useful to solve this. Write a function (or use a built-in one) that converts arrays to scalar values and checks if these values are unique in a set.

uniq = function(items, key) {
    var set = {};
    return items.filter(function(item) {
        var k = key ? key.apply(item) : item;
        return k in set ? false : set[k] = true;
    })
}

where key is a "hash" function that convert items (whatever they are) to comparable scalar values. In your particular example, it seems to be enough just to apply Array.join to arrays:

uniqueCoords = uniq(coordinates, [].join)

You can use standard javascript function splice for this.

for(var i = 0; i < coordinates.length; i++) {
    for(var j = i + 1; j < coordinates.length; ) {
        if(coordinates[i][0] == coordinates[j][0] && coordinates[i][1] == coordinates[j][1])
            // Found the same. Remove it.
            coordinates.splice(j, 1);
        else
            // No match. Go ahead.
            j++;
    }    
}

However, if you have thousands of points it will work slowly, than you need to consider to sort values at first, then remove duplicates in one loop.

I rewrote the answer from thg435 (It does not allow me to post comments) and prototype it also using jQuery instead, so this will work on all browsers using it (Even IE7)

Array.prototype.uniq = function (key) {
    var set = {};
    return $.grep(this, function (item) {
        var k = key
            ? key.apply(item)
            : item;
        return k in set
            ? false
            : set[k] = true;
    });
}

You can use it like:

arr = arr.uniq([].join);

If you are not on Safari this single liner could do the job

var arr = [[16.343345, 35.123523],
           [14.325423, 34.632723],
           [15.231512, 35.426914],
           [16.343345, 35.123523],
           [15.231512, 32.426914]],
    lut = {},
    red = arr.filter(a => lut[a] ? false : lut[a] = true);

document.write("<pre>" + JSON.stringify(red,null,2) + "</pre>");

It might be simpler to create another array keeping only unique coordinate pairs

var uniqueCoors = [];
var doneCoors = [];
for(var x = 0; x < coordinates.length; x++) {
    var coorStr = coordinates[x].toString();

    if(doneCoors.indexOf(coorStr) != -1) {
        // coordinate already exist, ignore
        continue;
    }

    doneCoors.push(coorStr);
    uniqueCoors.push(coordinates[x]);
}
function sortCoordinates(arr){
    var obj = {};
    for(var i = 0, l = arr.length; i < l; i++){
        var el = arr[i];
        var lat = el[0];
        var lng = el[1];

        if(!obj[lat + lng]){
            obj[lat + lng] = [lat, lng];
        } 
    }

    var out = [];
    for(p in obj){
        out.push([obj[p][0], obj[p][1]]);
    }
    return out;
}

I am not sure about coordinates[][] dataType. Make the comparison accordingly.

  var dubJRows= new Array();
  for(int i = 0; i <  coordinates.length -2; i++){
    for(int j = i+1; j <  coordinates.length -1; j++){
        if (i != j && chk_dubJRows_not_contains(j)) {
           innerArray1 [1][1] = coordinates[i];
           innerArray2 [1][1] = coordinates[j];
           if ( innerArray1 [1][0] == innerArray2[1][0] 
                   && innerArray1[1][1] == innerArray2[1][1]) {
               dubJRows.push(j);
            }
         }
       }
    }
    //REMOVE ALL dubJRows from coordinates.
发布评论

评论列表(0)

  1. 暂无评论