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 }
2.
var arr = [1,1,2,2,3,44,5,5,5]; for(var i = 0; i < arr.length; i++) { for(var j = i+1; j <arr.length; j++){ if (arr[i] == arr[j]){ arr.splice(j, 1); j--; //删除了以为,数组下标要改变 } } } alert(arr)