Reputation: 6684
I have this:
var mergeUniqueItems = ["-JsDEcxz_ZSGFLKwd1QM",
"-JsJ2NXGDYKI6QRsuXVK",
"-JsJ2RK-kOG2eGcG04xF",
"-JsJ2RK-kOG2eGcG04xF",
"-JsJ2YLPiP6751zh8geS"]
I have used this:
var duplicateArray = [];
for (var i = 0; i < mergeUniqueItems.length; i ++){
for (var j = 1; j < mergeUniqueItems.length; j ++){
if (mergeUniqueItems[i] == mergeUniqueItems[j]){
duplicateArray.push(mergeUniqueItems[i]);
}
}
}
console.log(duplicateArray);
Result has turn to be like this:
["-JsJ2NXGDYKI6QRsuXVK",
"-JsJ2RK-kOG2eGcG04xF",
"-JsJ2RK-kOG2eGcG04xF",
"-JsJ2RK-kOG2eGcG04xF",
"-JsJ2RK-kOG2eGcG04xF",
"-JsJ2YLPiP6751zh8geS"]
When my expectation is duplicate items in 1 array like below:
["-JsJ2RK-kOG2eGcG04xF"]
If there are more than one duplicate value, array should be like this:
["-JsJ2RK-kOG2eGcG04xF", "another_duplicate_1", "another_duplicate_2", ...]
I don't really know what's wrong with my code, please kindly help.
Thanks
Upvotes: 1
Views: 2453
Reputation:
Here's an alternate to Pointy's correct answer.
var duplicateArray = mergeUniqueItems.filter(function(item, i, orig) {
return orig.indexOf(item, i+1) === -1;
});
console.log(duplicateArray);
It's a good bit cleaner than using explicit loops.
Note that it'll retain the last instead of the first of the duplicates. If that's an issue, use .indexOf(item, i-1)
instead.
I may have been confused by what you needed. If you only needed a single instance of each duplicate, then it would be like this:
var duplicateArray = mergeUniqueItems.filter(function(item, i, orig) {
var nxt = orig.indexOf(item, i+1);
return nxt !== -1 && orig.lastIndexOf(item) == nxt;
});
Or this:
var duplicateArray = mergeUniqueItems.filter(function(item, i, orig) {
var nxt = orig.indexOf(item, i+1);
return nxt !== -1 && orig.indexOf(item, nxt+1) === -1;
});
Another way would be to use a separate .filter()
on the result of the first:
var duplicateArray = mergeUniqueItems.filter(function(item, i, orig) {
return orig.indexOf(item, i+1) !== -1;
}).filter(function(item, i, orig) {
return orig.indexOf(item, i+1) === -1;
});
Upvotes: 3
Reputation: 413702
Start j
off at i+1
instead of 1
, and check to make sure you've not already added the duplicate value.
for (var i = 0; i < mergeUniqueItems.length; i ++){
for (var j = i + 1; j < mergeUniqueItems.length; j ++){
if (mergeUniqueItems[i] == mergeUniqueItems[j]){
if (duplicateArray.indexOf(mergeUniqueItems[i]) < 0)
duplicateArray.push(mergeUniqueItems[i]);
break;
}
Upvotes: 3