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++){
//下標(biāo)為i的字符祟敛,把它作為數(shù)組的 key值码党,出現(xiàn)一次加 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>