3

I have an array as follows

var myArry = [[1,2], [1,1], [2,0], [1,2], [1,1]]

In this array i want find the duplicates as [1,1], [1,2] which are repeated. So how to do this in jquery/javascript.

Xyz
  • 5,955
  • 5
  • 40
  • 58
n92
  • 7,424
  • 27
  • 93
  • 129
  • 3
    jQuery won't help you here. What have you tried, have you searched? – Bergi Jan 08 '14 at 13:32
  • 3
    Combine [Easiest way to find duplicate values in a JavaScript array](http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array) with [Comparing two arrays in Javascript](http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript) – Bergi Jan 08 '14 at 13:35
  • 1
    If you open to [undersorejs](http://underscorejs.org/). You can use `_.uniq` and `_.difference`. Also see http://stackoverflow.com/questions/9923890/removing-duplicate-objects-with-underscore-for-javascript – Satpal Jan 08 '14 at 13:37

4 Answers4

4

Basically, you iterate over items and push each one into new array if it doesn't have it. The problem is how to compare different items, since object comparison like this would yield false:

[1,2] === [1,2]
// or this
[1,2] == [1,2]

One of the ways is to convert data into comparable format, perform iteration and then transform everything back:

var data = [[1,2], [1,1], [2,0], [1,2], [1,1]];

var noDupes = data
.map(function (item) {
  return JSON.stringify(item);
})
.reduce(function (out, current) { 
  if (out.indexOf(current) === -1) out.push(current);
  return out;
}, [])
.map(function (item) {
  return JSON.parse(item);
});

console.log(data, noDupes);

// [[1, 2], [1, 1], [2, 0], [1, 2], [1, 1]]
// [[1, 2], [1, 1], [2, 0]]

Obviously, with this approach you'll lose item reference. To keep them, use:

var data = [[1,2], [1,1], [2,0], [1,2], [1,1]];

var dataUnique = data.reduce(function (out, item) {
  return out.concat(out.filter(function (comp) {
    return item.toString() == comp.toString();
  }).length ? [] : [item])
}, []);

console.log(data, dataUnique);

// [[1,2], [1,1], [2,0], [1,2], [1,1]]
// [[1,2], [1,1], [2,0]]

But it works only if item doesn't contain any objects.

Update (26.01.2016):

Here's the ES2015 version:

const uniquePairs = pairs => [...new Set(pairs.map(pair => JSON.stringify(pair)))].map(pair => JSON.parse(pair))

uniquePairs([[1,2], [1,1], [2,0], [1,2], [1,1]])
// [[1,2],[1,1],[2,0]]

Step by step explanation:

  1. Convert pairs to comparable string. Note the arrow function used here:

    pairs.map(pair => JSON.stringify(pair)))

  2. Use an instance of Set to get only unique strings:

    new Set(pairs.map(pair => JSON.stringify(pair)))

  3. Convert Set to Array via spread operator:

    [...new Set(pairs.map(pair => JSON.stringify(pair)))]

  4. Convert it back to the initial pairs array structure:

    [...new Set(pairs.map(pair => JSON.stringify(pair)))].map(pair => JSON.parse(pair))

If you worry about browser ES2015 support, transpile it down to ES5 with Babel or similar tools.

2

try this:

function arraysEqual(arr1, arr2) {
    if(arr1.length !== arr2.length)
        return false;
    for(var i = arr1.length; i--;) {
        if(arr1[i] !== arr2[i])
            return false;
    }

    return true;
}

var duplicate = [];
$.each(myArry, function( index, value ) {
     $.each(myArry, function( index2, value2 ) {
          if(arraysEqual(value,value2))
               duplicate.push(value);
     });
});
dcodesmith
  • 9,590
  • 4
  • 36
  • 40
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
2
var myArray = [[1,2], [1,1], [2,0], [1,2], [1,1]], result = [];
var frequency = myArray.reduce(function(seen, currentItem) {
    if (currentItem in seen) {
        seen[currentItem] = seen[currentItem] + 1;
    } else {
        seen[currentItem] = 1;
    }
    return seen;
}, {});

for (var key in frequency) {
    if (frequency[key] > 1) {
        result.push(key.split(",").map(function(currentItem) {
            return parseInt(currentItem);
        }));
    }
}

console.log(result);

Output

[ [ 1, 2 ], [ 1, 1 ] ]
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
1

Loop through the array, compare the first and second values to every subsequent array entry.

If matching, store 1 copy of the match in a new array and return this as the result after the loop.

Sorry for no code, but this is pretty self-explanatory.

MaineCoder
  • 159
  • 1
  • 10