Teach you how to use charAt() in JavaScript to count the most frequently appearing characters and their number of occurrences

Teach you how to use charAt() in JavaScript to count the most frequently appearing characters and their number of occurrences

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');
    }

Core algorithm:

1. We can use the charAt() method to traverse the string.

2. Store each character in the object. If the object does not have this attribute, assign it a value of 1. If the attribute exists, add 1.

3. Traverse the object and get the maximum value and number of occurrences.

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.

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>

Summary: We can understand that all characters first execute obj[chars]=1, which is equivalent to creating an object, and then judging whether the object already exists.

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:
  • JavaScript counts the number of times a character appears
  • JavaScript counts the number of occurrences of each character in a string. Complete example
  • js code to count the number of characters that appear most often

<<:  MySQL 8.0.18 installation and configuration method graphic tutorial under MacOS

>>:  Detailed explanation of nginx forward proxy and reverse proxy

Recommend

js to call the network camera and handle common errors

Recently, due to business reasons, I need to acce...

Detailed introduction to MySQL database index

Table of contents Mind Map Simple understanding E...

Upgrade MySQL 5.1 to 5.5.36 in CentOS

This article records the process of upgrading MyS...

HTML left and right layout example code

CSS: Copy code The code is as follows: html,body{ ...

HTML+CSS+JavaScript to achieve list loop scrolling example code

Description: Set a timer to replace the content of...

Tips for writing concise React components

Table of contents Avoid using the spread operator...

Introduction to Apache deployment of https in cryptography

Table of contents Purpose Experimental environmen...

JavaScript message box example

Three types of message boxes can be created in Ja...

Detailed explanation of the correct way to install opencv on ubuntu

This article describes how to install opencv with...

How to install MySQL 5.7.17 and set the encoding to utf8 in Windows

download MySQL official download, select Windows ...

How to modify server uuid in Mysql

Source of the problem: If the slave server is the...

Detailed tutorial for installing MySQL on Linux

MySQL downloads for all platforms are available a...

Use nginx + secondary domain name + https support

Step 1: Add a secondary domain name to the Alibab...