Js searches for the characters and numbers that appear most frequently in strings. Example parsing: js string

Source: Internet
Author: User

Js searches for the characters and numbers that appear most frequently in strings. Example parsing: js string

For example, sssfgtdfssddfsssfssss, the most common character is s, and 12 times

Traditional writing

Analysis:

1. prepare an empty json string. The following figure shows that if this character is not in json, a new array is created in json and put the character into the array, if this character exists in json, add the character to the array. At this time, there are n arrays in json after the loop.

2. Find the longest array in json, and the maximum length is the number, and the most character is this parameter. The for... in... loop and the attr parameter are used.

Var str = "sssfgtdfssddfsssfssss"; function max () {var json ={}; var num = 0; var value = null; for (var I = 0; I <str. length; I ++) {var k = str [I]; if (! Json [k]) {json [k] = [];} json [k]. push (k); // else is not required here; otherwise, else is added only when this character exists. Less than once} for (var attr in json) {if (num <json [attr]. length) {num = json [attr]. length; value = json [attr] [0] ;}} alert ("the most common character is:" + value + ', number of occurrences:' + num );}; max (str );

What if I don't want to put anything in json?

Analysis:

1. Prepare an empty json string. The number of characters in the loop string is set to 1 if this character is not in json. If yes, the number is +

2. if a character in the cyclic json file exists, the number of characters is assigned to a variable, and the number of new characters and the size of the variable are compared each time. If it is larger than the variable, update the value of the variable. The value of the variable is the maximum number of characters.

The most common character is the json character.

Var str = "sssfgtdfssddfsssfssss"; function max () {var json ={}; for (var I = 0; I <str. length; I ++) {var k = str [I]; // k is all characters, the string is the same as the array. You can use the brackets subscript method to obtain this character in the if (json [k]) {json [k] ++; // json, set the number of this character to + 1,} else {json [k] = 1; // otherwise, set the number of this character to 1} var num = 0; var value = null; for (var k in json) {// s, f, g, t, dif (json [k]> num) {num = json [k]; value = k ;}} alert ("the most common character is:" + value + ', the number of occurrences is:' + num) ;}; max (str );

Regular Expression Method

Analysis:

1. Convert the string into an array for sorting, so that the regular expression can select the same character together.

2. Use the two parameters of the regular expression replace () method to match the maximum number of characters

Var str = "sssfgtdfssddfsssfssss"; var num = 0; var value = null; function max () {var new_str = str. split (""). sort (). join (""); var re =/(\ w) \ 1 +/g; // No \ 1, and re is a string in the whole sorting order, if \ 1 is enabled, it indicates that the previous item has been retrieved repeatedly. \ 1 indicates that new_str.replace (re, function ($0, $1) is the same as the previous one) {// $0 indicates that the whole is repeated, for example, [s, s...], [f, f...], [d, d...] $1 indicates the character if (num <$0. length) {num = $0. length; value = $1}); alert (value + ":" + num)}; max (str );

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.