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.
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.
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:
Convert pairs to comparable string. Note the arrow function used here:
pairs.map(pair => JSON.stringify(pair)))
Use an instance of Set to get only unique strings:
new Set(pairs.map(pair => JSON.stringify(pair)))
Convert Set to Array via spread operator:
[...new Set(pairs.map(pair => JSON.stringify(pair)))]
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.
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);
});
});
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 ] ]
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.