Useful LeetCode Library

Convert an integer into an array of digits:


var getDigitsArray = function(num){
    if(num === 0) return [0];
    var arr = [];
    while(num > 0){
        var digit = num % 10;
        arr.push(digit);
        num = parseInt(num/10);
    }
    return arr;
}

Convert an array into a hash map (Associative array object):


Array.prototype.toMap = function(){
    var map = {};
    this.forEach(x => {
        map[x] = x;
    });
    return map;
}

Convert an array into a hash map (key => count):


Array.prototype.toMap = function(){
    var map = {};
    this.forEach(x => {
        if(typeof map[x] === 'undefined') map[x] = 1;
        else map[x]++;
    });
    return map;
}