网心科技一面js 题目记录

378 阅读1分钟

今天下午去知春里地铁站附近一家叫网心科技面试,比较匆忙,面试的哥们问了一道js的方法题目:给一个字符串:“ashgahdbnvsxvjhgsxafsakjhkg99hhhhh99999999999999999hsakdhkehkewhutyyutqwewqhjhasgjhdasjhdgjhas”统计每个字母出现的次数,并输出最大的频率的字母。用笔纸张写的有点蒙蔽。

后来的冷静想了一下:

total_str ( str ){ 
            let temp= {},res=['',0]; 
            str.split('').map((item)=>{ 
                if(temp[item]!=undefined){ temp[item]+=1 } 
                    else { temp[item]=0 } }); 
            Object.keys(temp).map((i)=>{ if(temp[i]>res[1]){ res=[i,temp[i]]; } }) 
            console.log('res',res); 
            return res; 
        }

写的有点粗糙,勉强能用。