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