Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Created May 25, 2014 16:20
Show Gist options
  • Save jikeytang/986b745fa81087d0a225 to your computer and use it in GitHub Desktop.
Save jikeytang/986b745fa81087d0a225 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140526-题目2
查找字符串中出现最多的字符和个数。
如: addfbsdfaf,字母d出现的次数最多,且为4次。
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@zjhsd2007
Copy link

'addfbsdfaf'.split('').sort().join('').match(/(\w)\1+/g).sort(function(a,b){ return b.length - a.length})[0] //[ddd]

@rambo-panda
Copy link

var get_num_str = function(str){
    str = (str || 'addfbsdfaf').split('');
    var length = str.length-1,
        obj = {},
        cache;

    do{
        cache = str[length];
        obj[cache] = obj[cache] ? ++obj[cache] : 1;
    }while(--length>-1);

    for(cache in obj){
        cache = obj[cache];
        length = length > cache ? length : cache;
    }

    return {
        max_num : length,
        max_index : (function(){
                //  为了找出 重复的  比如 bbb  fff 都是3次
                for(cache in obj){
                    (obj[cache] === length)&& (str.push(cache));
                }
                return str;
        })(),
        num : obj
    }
        //  感谢zjhsd2007 (感觉 zjhsd2007 码兄 正则很牛逼啊 求拜师) 提供的思路。( 排序后 再正则)   不过我在测试特大数据的时候,还是我这个循环更有效   测试环境:chrome firefox   测试数据:长度为8000的26字母中的随机抓取组成的字符串
}

@mailzwj
Copy link

mailzwj commented May 26, 2014

function getCharAndCount(str){
    var len = str.length; i = 0, lenArr = [], max = 0;
    while(i < len){
        lenArr.push({n: str.split(str[i]).length - 1, c: str[i++]});
    }
    max = lenArr.sort(function(a, b){return b.n - a.n;})[0];
    return {char: max.c, count: max.n};
}
getCharAndCount("ababbc"); // {char: "b", count: 3}

@rambo-panda
Copy link

@mailzwj 方法 多次使用split 以及 str[i++] 多次隐式转换成Array再进行取值。 对长数据 不会有影响吗?

@hjzheng
Copy link

hjzheng commented May 27, 2014

ES5的forEach

function test(arr){
    var uniqArr = [];
    var obj = {};
    arr.forEach(function(item){
        if(uniqArr.indexOf(item) == -1){
            uniqArr.push(item);
            obj[item] = 1;
        } else {
            obj[item] ++;
        }
    });

    uniqArr.sort(function(a, b){
        return obj[b] - obj[a];
    });
    return {
        str: uniqArr[0],
        num: obj[uniqArr[0]]
    };
}

console.log(test("addfbsdfaf".split("")));

@styling
Copy link

styling commented May 30, 2014

function a (a) {
    var x, y, z;
    for(var i = 0; i < a.length; i++){
        y = a.split(a[i]).length;
        if(!x || y > x){
            x = y;
            z = a[i];
        }       
    }
    return {
        str : z,
        length : x
    }
}

@jiangtao
Copy link

jiangtao commented Jun 6, 2014

function getStrMore2(str){

    var getStr,
        oldStr,
        res = {},
        max = 0;
    while(str){
        getStr = str.slice(0,1);
        oldStr = str;
        str = str.replace(new RegExp(getStr, 'g'), '');
        if(max < oldStr.length - str.length){
            max = oldStr.length - str.length;
            res['target'] = getStr;
            res['max'] = max;
        }
    }
    return res;
}

getStrMore2('addafsfe');

@jiangtao
Copy link

jiangtao commented Jun 6, 2014

@zjhsd2007 字符串太长的话 sort效率好低

@wsgouwan
Copy link

wsgouwan commented Dec 4, 2014

var str = 'addfbddsdfaf';
        var nStr = str.split('').sort().join('');   
        var re = /(\w)\1*/g;   //

        var tempArr = nStr.match(re), temp = 0 ,index = 0;
        for(var i =0; i < tempArr.length ; i ++){
            if( tempArr[i].length > temp){
                temp = tempArr[i].length ;
                index = i ;
            }
        }
        console.log( '字母'+tempArr[index][0]+'出现的最多,you'+temp+'次' )
@ljkfgh2008  你的好像有问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment