Detecting Duplicates In An Array In JavaScript

I came across this problem and thought I would share my solution.

I did a bit of Google-Fu and found a pretty good answer here:

Stack Overflow post.

However, I just wanted to know if any duplicates EXISTED. I didn’t care about what they were, so I adapted the solution below:

    var checkForDuplicateValues = function(theArray) {
        var placeHolder = theArray.length,
            comparePlaceHolder;

        while(comparePlaceHolder=--placeHolder) {
            while(comparePlaceHolder--) {
                if(!(theArray[placeHolder]!== theArray[comparePlaceHolder])) {
                    return true;
                }
            }
        }

        return false;
    };

I hope this helps! As always leave a comment to provide constructive feedback!

Don Marges

Don Marges

Fullstack Web Developer

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin