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
Recently, due to business reasons, I need to acce...
Table of contents Mind Map Simple understanding E...
This article records the process of upgrading MyS...
CSS: Copy code The code is as follows: html,body{ ...
Description: Set a timer to replace the content of...
Table of contents Avoid using the spread operator...
Table of contents Purpose Experimental environmen...
Three types of message boxes can be created in Ja...
This article describes how to install opencv with...
MySQL error: Error code: 1293 Incorrect table def...
download MySQL official download, select Windows ...
Source of the problem: If the slave server is the...
MySQL downloads for all platforms are available a...
Why is the title of the article “Imitation Magnif...
Step 1: Add a secondary domain name to the Alibab...