Home  >  Article  >  Web Front-end  >  Two ways to get the most repeated item in a string using JS

Two ways to get the most repeated item in a string using JS

巴扎黑
巴扎黑Original
2016-11-25 15:16:501442browse

The first method is more cumbersome, and the second method is simpler

First method:

Basic idea:

1. First merge the repeated items in the string and obtain the repeated item names and the number of repetitions in the form of objects;

2. Sort according to the number of repetitions of each item, that is, the most repeated item name and number of repetitions are obtained

Direct code:

<script>
    var str = &#39;85skkkhj22gdg541232286oe45854664445sada2fasa51233148dskf7212772&#39;;
    var res = countSort(str); //归总为对象素组
    rr = sortArrayByItem(res, &#39;len&#39;); //根据每一项的length属性排序
    var maxCount = rr[0];
    console.log(&#39;重复最多的项是:&#39; + maxCount.item + "---重复的次数是:" + maxCount.len);
    //字符串重复项的归总(以对象数组的形式)
    function countSort(str) {
        var array = str.split(&#39;&#39;);
        var filter = [];
        var result = [];
        //查找与tar相同的所有项,返回最终一位数组集合arr
        var get = function (str, tar, arr, tmp) {
            if (str.indexOf(tar) >= 0) {
                var tmp = str.slice(str.indexOf(tar) + 1);
                arr.push(tar);
                get(tmp, tar, arr, tmp);
            }
            return arr;
        }
        for (i in array) {
            var elm = [];
            var tmp;
            var fstr = filter.join(); //已完成归并的所用项
            if (fstr.indexOf(array[i]) >= 0)
                continue;
            else {
                var tmp_arr = get(str, array[i], elm, tmp); //完成的一项归总
                result.push({
                    item: tmp_arr[0]
                    , len: tmp_arr.length
                });
                filter.push(array[i]);
            }
        }
        return result;
    }
    //对象数组的排序,item代表要根据那个属性来做排序(从大到小)
    function sortArrayByItem(array, item) {
        for (var i = 0; i < array.length - 1; i++) {
            for (var j = i + 1; j < array.length; j++) {
                if (array[i][item] < array[j][item]) {
                    var tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
        return array;
    }
</script>

Second type:

Basic idea:

1. Combine each item in the string and the number of times it appears to form an object;

2. Compare the obtained objects according to their values ​​and find the largest term;

<script>
 var tt = Char(&#39;dj84dccvdda85454kk444gghg6675786fh&#39;);
    console.log(tt)
    tt = maxC(tt);
    console.log(tt)
    function Char(str) {
        var uchars = {};
        str.replace(/\S/g, function (l) {
            uchars[l] = (isNaN(uchars[l])) ? 1 : uchars[l] + 1
        })
        return uchars
    }
    function maxC(obj) {
        var maxCount = 0;
        var maxItem = null;
        for (var i in obj) {
            if (obj[i] > maxCount) {
                maxCount = obj[i];
                maxItem = i;
            }
        }
        return {
            maxCount: maxCount
            , maxItem: maxItem
        };
    }
</script>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn