JS to obtain the most repeated items in the string.

Source: Internet
Author: User
The first method is relatively cumbersome, and the second method is relatively simple. The first method is as follows: 1. first, the repeated items in the string are merged to obtain the repeated item names and repeated times in the form of objects; 2. root... the first method is cumbersome, and the second method is simple.

First:

Basic Ideas:

1. Merge repeated items in the string to get the repeated item names and repeated times in the form of objects;

2. sort by the number of repeated items, that is, the maximum number of repeated items and the number of repeated items are obtained.

Directly run the Code:

Script var str = 'hangzhou'; var res = countSort (str); // returns to the object group rr = sortArrayByItem (res, 'len '); // sort var maxCount = rr [0] based on the length attribute of each item; console. log ('the most repeated items are: '+ maxCount. item + "--- repeated times:" + maxCount. len); // returns the repeated string items (in the form of an array of objects) function countSort (str) {var array = str. split (''); var filter = []; var result = []; // search for all the same items as tar, returns the final array set 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); // The result of a completed item. push ({item: tmp_arr [0], len: tmp_arr.length}); filter. push (array [I]) ;}} return result ;}// sorting of the object array. item indicates the attribute to be sorted (from large to small) 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:

Basic Ideas:

1. the number of times each item in the string appears as an object;

2. Compare the obtained objects by value to find out the condition items;

《script》 var tt = Char('dj84dccvdda85454kk444gghg6675786fh');    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》

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.