Nodejs Array Has Duplicate hasDuplicates()

Here you can find the source of hasDuplicates()

Method Source Code

//http://stackoverflow.com/questions/19655975/check-if-an-array-contains-duplicate-values
Array.prototype.hasDuplicates = function() {
    this.sort();//  w  w w  . ja v a 2 s.  c o  m
    for (var i = 1; i < this.length; i++) {
        if (this[i - 1] == this[i]) {
            return true;
        }
    }
    return false;
};

Related

  1. hasDuplicates()
    Array.prototype.hasDuplicates = function() {
        for (var i = 0; i < this.length; i++) {
            var currentVal = this[i];
            for (var j = 0; j < this.length; j++) {
                if(i === j) continue; 
                var val = this[j];
                if (currentVal === val) {
                    return true;
        return false;
    
  2. isDuplicate(val)
    Array.prototype.isDuplicate = function(val) {
        for (var i = 0; i < this.length; i++) {
            var currentVal = this[i];
            if (currentVal === val) {
                return true;
        return false;