字符串排重,并输出重复出现次数最多的的字符和重复字数

var string = 'abcdaaabcd';

var arr = string.split("");

stringOk = Array.from([...new Set(arr)]).join('');

var ele = document.getElementsByClassName('text')[0];

ele.innerHTML = ' 排重后的字符串' + stringOk;

 

function sortString(data) {

let obj = {}

for (let i of data) {

if (!obj[i]) {

obj[i] = 1

} else {

obj[i] += 1

}

}

return obj

}

var max = null;

var dataCount = sortString(arr);

var sortData = Object.values(dataCount).sort((a, b) => b - a);

var key = null;

for (let item in dataCount) {

if (dataCount[item] == sortData[0]) {

key = item

}

}

document.getElementsByClassName('hhh')[0].innerHTML = "重复率最大" + key + "次数" + sortData[key]

你可能感兴趣的:(it,前端)