Previously, I introduced the use of the charAt() method in JavaScript. Today, I will share with you the example code of js to count the characters and times with the highest number of times. The specific content is as follows: Determine the character that appears most frequently in a string 'aabcdobdackoppz' and count its number of occurrences First we need to understand a knowledge point: (how to determine whether the object has this attribute) // There is an object to determine whether it has this attribute // Writing method: object['attribute name'] var o = { age : 18 , sex : 'male' } if(o['sex']){ console.log('This property exists'); } else { console.log('No such property'); }
1. Use the charAt method to traverse the array 'aabcdobdackoppz' (which can be understood as storing the value in the object obj) <script> var str = 'aabcdobdackoppz' ; var obj = {} ; for(var i = 0 ; i < str.length ; i++){ var chars = str.charAt(i) // chars is each character of the string if(obj[chars]){ // o[chars] gets the attribute obj[chars]++ } else { obj[chars] = 1 ; } } console.log(obj); </script> At this point, the value has been stored in the obj object. 2. Next, you just need to traverse the object to get the maximum value and number of occurrences <script> //Traverse the object var max = 0; var ch = ''; for (var k in obj) { // To traverse an object, you need to use for in // k gets the attribute name, obj[k] gets the attribute value // The attribute name k here is only useful in the loop, so we need to create a ch to store k if (obj[k] > max) { max = obj[k]; ch = k; } } console.log(ch + 'The most frequent occurrence' + 'The number of occurrences is:' + max); </script>
This is the end of this article about using charAt() in JavaScript to count the most frequently appearing characters and their number of times. For more information about how to count the most frequently appearing characters and their number of times in js, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL 8.0.18 installation and configuration method graphic tutorial under MacOS
>>: Detailed explanation of nginx forward proxy and reverse proxy
Preface: MYSQL should be the most popular WEB bac...
There are three main ways of MySQL replication: S...
Table of contents Install Tomcat with Docker Use ...
I encountered this problem when I was making the ...
<br />When you click the link, the web page ...
:is dynamic component Use v-bind:is="compone...
Vue methods and properties 1. Methods Usage 1 met...
Table of contents environment summary Module Func...
Perfect solution to VMware black screen after Mac...
Introduction to Positioning in CSS position attri...
Table of contents 1. Install the proxy module 2. ...
Anaconda is the most popular python data science ...
Red and pink, and their hexadecimal codes. #99003...
Table of contents 1. Introduction: 2. Docker: 1 C...
Because using group by in MySQL always results in...