美文网首页
找出指定字符串中重复最多的字符

找出指定字符串中重复最多的字符

作者: 冷r | 来源:发表于2019-07-30 11:28 被阅读0次
    function test(str){
          let _length = 0;
          let result = [];
          let str_list = str.split('');
          let _result = new Set(str_list);
          for (let w of _result) {
             let number = str_list.filter(element => element === w).length;
             if (number > _length) {
                result = [
                   {
                      _length: number,
                      word: w
                   }
                ];
                _length = number;
             } else if (number === _length) {
                result.push({
                   _length: number,
                   word: w
                });
             }
          }
          return result
    }
    
     test("a eh sjdh s d");//[{_length: 2, word: "h"},{_length: 2, word: "s"},{_length: 2, word: "d"}]
    

    相关文章

      网友评论

          本文标题:找出指定字符串中重复最多的字符

          本文链接:https://www.haomeiwen.com/subject/flmgrctx.html