For an interview question, find the character that appears most frequently in the string in js

<script>
var str = "zhaochucichuzuiduodezifu";
var o = {};
for (var i = 0, length = str.length; i < length; iPP) {
// var char = str[i];
var char = str.charAt(i);
if (o[char]) { //charoo[char]o[char]
o[char]PP; //1
} else {
o[char] = 1; //1
}
}
console.log(o); //
//
var max = 0;
var maxChar = null;
for (var key in o) {
if (max < o[key]) {
max = o[key]; //max
maxChar = key; //key
}
}
console.log("" + maxChar);
console.log("" + max);
</script>

A method found on the Internet
in which if (o [char]) I don"t understand here. If the simple output o [char] is sure why undefined executes else to make it 1 and then executes it only once. Why the final output of a 0 object is really not clear. This is how the key vaule is added to this object

Jun.06,2021

o [char] records the number of occurrences of each element.
for example, if the first element is a, if o ['a'] is undefined, then you will go else o ['a'] = 1 and record it once.
followed by the an of the element, o ['a'] is true and the number of times to go if o ['a'] PP, will be increased once!


to understand that the object of js is the relationship of {key,value}. If key does not exist, then o.key is undefined (if. If o.key exists, the corresponding data is value. And o.key = 1 means to add a key, corresponding to 1.
say for the first time, o [z'], that is, o.z must be undefined, to the else branch, and then o.z equals 1. The next time you encounter the'z 'string, you will reach the branch with the number of times plus 1

.
if (o[char]) { 
o[char]PP; //1
} else {
o[char] = 1; //1
}

var str = "zhaochucichuzuiduodezifu";
var strCounter = {};
var maxCount = 0;
var resC = '';
for (var i in str) {
    var c = str[i];
    strCounter[c] ? strCounter[c]PP : (strCounter[c] = 1);
    if (strCounter[c] > maxCount) {
        maxCount = strCounter[c];
        resC = c;
    }
}

console.log('strCount', strCounter);
console.log('resC', resC);

//
        var str = 'asdsdddd';
        console.log(str)
        var obj = {};
        for(var i=0;i<str.length;iPP){
            var strIndex = str.charAt(i);
            if(obj[strIndex]){
                obj[strIndex]PP;
            }else{
                obj[strIndex] = 1;
            }
        }
        console.log(obj)
        var max = 0;
        for(var key in obj){
            if(max < obj[key]){
                max = obj[key]
            }
        }
        for(var key in obj){
            if(obj[key] == max){
                console.log(''+key);
                console.log(''+max);
            }
        }

function countMaxStr(str){
    var o = {}
    str.split('').forEach(item=>{
        if(item in o){
            o[item]PP
        }
        else {
            o[item] = 1
        }
    })
    return sortObj(o)
}

function sortObj(obj){
    var arr = []
    for(var key in obj){
        arr.push({
            key:key,
            value:obj[key]
        })
    }
    arr.sort((a,b)=>{
        return a.value > b.value
    })
    return arr.pop()
}

console.log(countMaxStr("121313fefefrg"))
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e596d4-44a74.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e596d4-44a74.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?