Nodejs Array Has Duplicate hasDuplicates()

Here you can find the source of hasDuplicates()

Method Source Code

/**// w w w. j  a  va2  s .co  m
 * Unit tests:
 * test/js/ArrayTest.js
 */

/**
 * Search for duplicate value.
 * - Shallow search
 * - Compares primitives only
 *
 * return - True if this array has duplicated values.
 */
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; /* Same object, ignore it. */

            var val = this[j];
            if (currentVal === val) {
                return true;
            }
        }
    }
    return false;
}

Related

  1. hasDuplicates()
    Array.prototype.hasDuplicates = function() {
        this.sort();
        for (var i = 1; i < this.length; i++) {
            if (this[i - 1] == this[i]) {
                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;