function maxLength(str){ var arr = str.split(''); var max = { item:[], count:1 }; var count = 1 for(var i=1;i<arr.length;i++){ if(arr[i] == arr[i-1]){ count++; }else{ if(max.count<count){ max.item = [] max.item.push(arr[i-1]); max.count = count }else if(max.count==count && max.item.indexOf(arr[i-1])==-1){ max.item.push(arr[i-1]) max.count = count } count = 1; } } return max.item + '出现相同连续最长,次数为' + max.count + '次'; } console.log(maxLength('sdsaaaaacsaaaaaaaackkkkkkkkdddd')) //a,k出现相同连续最长,次数为8次