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

21 MySQL standardization and optimization best practices!

Preface Every good habit is a treasure. This arti...

MySQL 5.7.15 version installation and configuration method graphic tutorial

This article shares with you a detailed tutorial ...

How to implement mask layer in HTML How to use mask layer in HTML

Using mask layers in web pages can prevent repeat...

Meta tags in simple terms

The META tag, commonly referred to as the tag, is...

Vue implements simple comment function

This article shares the specific code of Vue to i...

CSS3 uses scale() and rotate() to achieve zooming and rotation

1. scale() method Zoom refers to "reducing&q...

Summary of Vue watch monitoring methods

Table of contents 1. The role of watch in vue is ...

How to use docker compose to build fastDFS file server

The previous article introduced a detailed exampl...

How to manually upgrade the kernel in deepin linux

deepin and Ubuntu are both distributions based on...

Detailed explanation of how to view the current number of MySQL connections

1. View the detailed information of all current c...

How to correctly modify the ROOT password in MySql8.0 and above versions

Deployment environment: Installation version red ...