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"))