字符串中求出现最多的字符和个数
作者:
思念LY | 来源:发表于
2018-04-23 11:32 被阅读4次vue.js
<template>
</template>
<script>
export default {
mounted:function(){
this.getmaxstr();
},
methods:{
getmaxstr: function(){
let str = 'aabbcc';
let newarr = []; //形式[a:1,b:2]
for(let i = 0; i<str.length; i++){
//下标为i的字符,把它作为数组的 key值,出现一次加 1
if(!newarr[str.charAt(i)]){
newarr[str.charAt(i)] = 1;
} else {
newarr[str.charAt(i)] += 1;
}
}
let maxValue = 0;
let maxKey = '';
for(let v in newarr){
if(newarr[v] > maxValue){
maxValue = newarr[v];
maxKey = v;
}
}
console.log(maxKey +':' + maxValue)
for(let v in newarr){
if(newarr[v] == maxValue && v!==maxKey){
console.log(v +':' + newarr[v])
}
}
}
},
data () {
return {
}
}
}
</script>
本文标题:字符串中求出现最多的字符和个数
本文链接:https://www.haomeiwen.com/subject/hrbklftx.html
网友评论