The first kind of
var str = "aaasssksldnfkjr"
var o = {}
for (var i = 0; i < str.length; i++) {
var name = str[i]
if (o[name]) {
o[name]++
} else {
o[name] = 1
}
}
var max = 0;
var maxs = ""
for (var name in o) {
if (max < o[name]) {
max = o[name];
maxs = name
}
}
console.log(max, maxs)
Copy the code
Second method of regularization
var str = "aaasssksldnfkjr"
var arr = str.split("")
var Arr = arr.sort()
var s = Arr.join("")
console.log(s)
var max = 0;
var maxs=""
s.replace(/(\w)\1+/g,function(s,s2){
if(s.length>max){
max=s.length;
maxs=s2
}
})
console.log(max, maxs)
Copy the code
The third kind of
var str = "aaasssksldnfkjr" var arr = str.split("") var Arr = arr.sort(); var max=0; var maxs=""; var n=1 for(var i=0; i<Arr.length; i++){ if(Arr[i]==Arr[i+1]){ n++ }else{ if(n>max){ max=n; maxs=Arr[i] } n=1 } } console.log(max, maxs)Copy the code