1.
var str = "123456654123789999999879999" findMostAppearNum(str); function findMostAppearNum(str) { str = str.split(‘‘).sort().join(‘‘) var re = /(\w)\1+/g; var value = ‘‘; var count = 0; str.replace(re, function ($0, $1) { if (count < $0.length){ count = $0.length; //$0 每个重复项 value = $1; //重复项的具体值 } }); alert(‘出现次数最多的字符是:‘+ value+"共出现了:"+count+"次") } var str1 = "a a b a b e"; var str2 = "123456654123789999999879999" console.log(removeDup(str1)); console.log(removeDup(str2)); function removeDup(str) { var arr = str.trim().split("").sort(); var len = arr.length - 1; for (var i = len; i > 0; i--) { if (arr[i] == arr[i - 1]) { arr.splice(i, 1); // 后面和前面的一样删除后面的,如此反复 } } return arr }
时间: 2024-12-29 11:45:50