A simple example of determining the maximum number of characters and the number of occurrences of a string in JS. js string

Source: Internet
Author: User

A simple example of determining the maximum number of characters and the number of occurrences of a string in JS. js string

A simple example of determining the maximum number of characters and the number of occurrences of a string in JS

<Script type = "text/javascript"> var str = 'qwertyuilo ., mnbvcsarrrrrrtyuiop; l, mhgfdqrtyuio ;. cvxsrtyiuo '; var json ={}; // traverses str and disconnects each character to take out the value and number of occurrences of a specific character as the json kv for (var I = 0; I <str. length; I ++) {// determines whether the current str value exists in json if (! Json [str. charAt (I)]) {// Add the current value to json if it does not exist [str. charAt (I)] = 1;} else {// else, the index value of the existing value in the array will be +; json [str. charAt (I)] ++ ;}// store the maximum number of occurrences and times var number = ''; var num = 0; // traverse json and use the hitting algorithm to calculate the required value for (var I in json) {// if the current item is greater than the next if (json [I]> num) {// change the current value to the value num = json [I]; number = I ;}} // print the maximum number of occurrences and the number of occurrences of alert ('the maximum number of occurrences is '+ number +' and the number of occurrences is '+ num); </script>

A simple example of judging the most frequently occurring characters and the number of occurrences of a string in the above JS is all the content shared by xiaobian. I hope you can give me a reference, we also hope that you can support the customer's home.

Related Article

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.