JavaScript writes a piece of code that determines the most frequently occurring string in a string and counts the number of occurrences

Source: Internet
Author: User

JavaScript writes a piece of code that determines the most frequently occurring string in a string and counts the number of occurrences

1 functionTest () {2     varBT = document.getElementById ("BT"));3Bt.addeventlistener ("click",function(){ 4 5         varstr = "dafdsjkfnaiesdaadsllllllkkkkk444444444444444";6         varobj = [];//holds an array of result sets, storing Key-value objects in the results7          for(varI =0;i<str.length;i++) {//iterating through a string to find a result set8             varKey =Str[i];9             if(!Obj[key]) {TenObj[key] = 1;//If the character is not added to the result set assigned initial 1 One}Else{ Aobj[key]++;//number of characters added in the result set + + -             } -         } theConsole.log (typeofobj); -          for(Keyinchobj) { -Console.log (key+Obj[key]); -Console.log (typeofkey); +Console.log (typeofObj[key]); -         } +          A         varResultkey=[];//one or more characters (with multiple occurrences of the same number of characters) stored the most at         varmax =-1;//Maximum number of occurrences -          for(Keyinchobj) {//traversing result sets -             if(Obj[key]==max) {//multiple characters - Resultkey.push (key); -             } -              in             if(Obj[key]>max) {//a character -Resultkey = [];//The maximum number of character arrays is empty toResultkey.push (key);//adding characters +Max =Obj[key]; -             } the         } *Console.log ("Resultkey:" +resultkey+ "" + "Count:" +max); $ Panax Notoginseng     } ); -}
View Code

JavaScript writes a piece of code that determines the most frequently occurring string in a string and counts the number of occurrences

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.