1

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

1
  • Your description is a little confusing. Should the result keep or exclude the duplicates. Your code looks like it should keep dupes (except for the last dupe), and I'm not entirely understanding the results you show. Commented Jun 21, 2015 at 17:06

2 Answers 2

3

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;
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Why can't I accept the answer in less than 10 minutes? Your answer is correct sir. Thanks.
@Vicheanak: Because sometimes what seems to be a correct answer can be wrong, misleading, or even dangerous. A small time limit allows for a few more people to look at the answer and react if necessary.
3

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;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.