关于一道面试题,js中查找字符串中出现次数最多的字符

<script>

var str = "zhaochucichuzuiduodezifu";

var o = {};

for (var i = 0, length = str.length; i < length; i++) {

// var char = str[i];

var char = str.charAt(i);

if (o[char]) { //char就是对象o的一个属性,o[char]是属性值,o[char]控制出现的次数

o[char]++; //次数加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>


网上找到的一种方法 

其中 if (o[char]) 这里没看懂。如果单纯的输出o[char]肯定为什么undefined执行了else使他为1然后执行 也只执行一次。 为什么最后会输出一个0的对象着实不明白。这个key vaule是这么加入到这个对象里的


慕运维8079593
浏览 1433回答 5
5回答

红颜莎娜

o[char]就是记录每个元素出现的次数的。例如第一个元素是a,走到这里 o['a']是undefined 那就会走else o['a'] = 1,记录一次。后面再有元素的a的话 o['a'] 就为真了 走if o['a']++,次数就会增加一次了!

jeck猫

要理解下js的对象,是{key,value}的关系的,如果key不存在, 则o.key就是undefined(if判断里面就是false), 如果存在o.key,对应的数据就是value。 而o.key = 1的意思就是给o添加一个key,对应的值是1.拿第一次说,o['z'],就是o.z 肯定是undefined,就到了else 分支,之后o.z就等于1。下次再碰到'z'字符串,就到了次数加1的分支if (o[char]) {&nbsp;o[char]++; //次数加1} else {o[char] = 1; //若第一次出现,次数记为1}

守着一只汪

var str = "zhaochucichuzuiduodezifu";var strCounter = {};var maxCount = 0;var resC = '';for (var i in str) {&nbsp; &nbsp; var c = str[i];&nbsp; &nbsp; strCounter[c] ? strCounter[c]++ : (strCounter[c] = 1);&nbsp; &nbsp; if (strCounter[c] > maxCount) {&nbsp; &nbsp; &nbsp; &nbsp; maxCount = strCounter[c];&nbsp; &nbsp; &nbsp; &nbsp; resC = c;&nbsp; &nbsp; }}console.log('strCount', strCounter);console.log('resC', resC);

慕容3067478

//判断一个字符串中出现次数最多的字符,并统计这个次数&nbsp; &nbsp; &nbsp; &nbsp; var str = 'asdsdddd';&nbsp; &nbsp; &nbsp; &nbsp; console.log(str)&nbsp; &nbsp; &nbsp; &nbsp; var obj = {};&nbsp; &nbsp; &nbsp; &nbsp; for(var i=0;i<str.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var strIndex = str.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(obj[strIndex]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj[strIndex]++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj[strIndex] = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; console.log(obj)&nbsp; &nbsp; &nbsp; &nbsp; var max = 0;&nbsp; &nbsp; &nbsp; &nbsp; for(var key in obj){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(max < obj[key]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = obj[key]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(var key in obj){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(obj[key] == max){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('出现次数最多的是'+key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('次数是'+max);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }

婷婷同学_

function countMaxStr(str){&nbsp; &nbsp; var o = {}&nbsp; &nbsp; str.split('').forEach(item=>{&nbsp; &nbsp; &nbsp; &nbsp; if(item in o){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o[item]++&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o[item] = 1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })&nbsp; &nbsp; return sortObj(o)}function sortObj(obj){&nbsp; &nbsp; var arr = []&nbsp; &nbsp; for(var key in obj){&nbsp; &nbsp; &nbsp; &nbsp; arr.push({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key:key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value:obj[key]&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; &nbsp; arr.sort((a,b)=>{&nbsp; &nbsp; &nbsp; &nbsp; return a.value > b.value&nbsp; &nbsp; })&nbsp; &nbsp; return arr.pop()}console.log(countMaxStr("121313fefefrg"))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript