标签:iter javascrip 思维 语法 ack AC es6 res nbsp
一、数组去重的方式
//1循环一次
var arr1 = [1,2,3,44,44,11,2,3,333,3,3]; function removeRepeat(arr){ var removeArr = [],obj = {}; for(var i = 0, l = arr.length; i < l; i++){ if(!obj[arr[i]]){ removeArr.push(arr[i]); obj[arr[i]] = 1; } }; return removeArr; } var singleArr = removeRepeat(arr1); console.log(singleArr); //[1, 2, 3, 44, 11, 333]
function unique(arr){
var obj = {}
var result = []
for(var i in arr){
if(!obj[arr[i]]){
obj[arr[i]] = true;
result.push(arr[i]);
}
}
return result;
}
function remove2(arr){ return Array.from(new Set(arr)); } var single2 = remove2(arr1); console.log(single2) //[1, 2, 3, 44, 11, 333] es6 语法 其实问数组的去重主要是考上面一种思维方式
二、返回字符串中字符出现次数最多的那字符
var str = "abbcccddddfffffgggggg"; function maxNum(str){ if(str.length <= 1){ return str; } var a1 = str.split(‘‘),obj = {},result = []; //利用上面一个数组去重的思想 for(var k = 0, l = a1.length; k < l; k++){ if(!obj[a1[k]]){ obj[a1[k]] = 1; }else { obj[a1[k]] += 1; }; } var num = 0, word = ""; for(var item in obj){ if(obj[item] > num){ num = obj[item]; word = item; } } return word; } var w = maxNum(str); console.log(w); //g
标签:iter javascrip 思维 语法 ack AC es6 res nbsp
原文地址:https://www.cnblogs.com/rubyHelloWorld/p/9037957.html