获取字符串中出现次数最少的字符

转自:https://www.cnblogs.com/yugege/p/6526215.html#!comments

function getRareChar(str) {
    let hash = {};
    // 将各个字符名字、首次出现位置及出现次数存到hash表
    for(let i = 0, len = str.length; i < len; i++) {
        // 如果hash[str[i]]不存在,则对其进行初始化
        hash[str[i]] = hash[str[i]] || {index: i, count: 0};
        hash[str[i]].count++; // count计数自增
    }
    // 因为哈希表不好排序,将它转成数组
    return Object.keys(hash).map(function (key) {
       return Object.assign({char: key}, hash[key]);
    // 根据count属性进行升序排序
    }).sort(function (a, b) {
        return a.count - b.count;
    // 取出count最小的
    }).filter(function (e, i, arr) {
        return e.count === arr[0].count;
    // 在count值最小的集合里面再根据index属性进行升序排序
    }).sort(function (a, b) {
        return a.index - b.index;
    })[0].char;
}

 

posted @ 2020-05-21 14:09  justsilky  阅读(654)  评论(0编辑  收藏  举报