Javascript Array dominator()

Description

Javascript Array dominator()


'use strict'//www .  j ava 2  s.c o  m


Array.prototype.dominator = function() {
    let o = {}, i, l = this.length;

    let threshold = l / 2;
    let ret = -1;

    for (i = 0; i < l; i += 1) {
        o[this[i]] = parseInt(o[this[i]]) + 1 || 1;

        if (threshold < o[this[i]]) {
            ret = i;
            break;
        }

    }
    console.log(ret)
    return ret;


    };


return A.dominator();
}

solution([1,2,33,33,33,33,33,5]);



PreviousNext

Related