JavaScript 如何获取指定字符串中每个字母的发生次数

JavaScript 如何获取指定字符串中每个字母的发生次数

在本文中,我们给出一个字符串,我们的任务是使用用户定义的函数找到字符串中字符的出现次数。

Example:
Input : "hello"
Output : h occur 1 times
         e occur 1 times
         l occur 2 times
         o occur 1 times
Explanation : here "hello" have 1 h, so it have 1 value.
               as same e have 1, l have 2 , o have 1.
Example 2:
Input : "did"
Output: d occur 2 times 
i occur 1 times

方法1

在这种方法中,我们使用一个映射数据结构来存储字符出现的次数。

  • 首先,我们初始化映射,为字符串的每个字符设置一个键,并将每个键的值设为0。
  • 我们遍历字符串,并递增字符的值。
  • 最后,打印映射的键值对。

示例: 这个示例展示了上述方法的使用。

<script>
    //function to print occurrence of character
    function printans( ans )
    {
      for( let [ key ,value] of ans)
      {
        // if()
        console.log(`{key}  occurs {value} times` );
           
      }
     
    }
     
    // function count occurrence of character
    function count( str , outp_map )
    {
      for( let i = 0 ;i < str.length ;i++)
      { 
     
        let k = outp_map.get(str[i]);
        outp_map.set(str[i], k+1) ;
             
         
      }
      //calling  print function
      printans(outp_map);
    }
     
    //function create map to count character
    function count_occurs( test , callback )
    {
      //checking string is valid or not 
      if( test.length === 0 )
      {
        console.log(" empty string ");
        return ;
      }
      else
      { 
        // map for storing count values
        let ans = new Map();
        for( let i = 0 ;i < test.length;i++)
        {
          ans.set(test[i], 0);
        }
         
        callback( test ,ans);
         
      }
     
    }
     
    // test string 
    let test =  "helloworld";
    count_occurs( test ,count);
</script>

输出:

h  occurs  1 times
e  occurs  1 times
l  occurs  3 times
o  occurs  2 times
w  occurs  1 times
r  occurs  1 times
d  occurs  1 times

方法2

在这个方法中,我们使用嵌套的for循环来遍历字符串,并计算字符串中每个字符的出现次数。

  • 首先,为字符串的第i个值初始化count为0。
  • 然后,如果第i个值与字符匹配,增加count的值1。
  • 最后,打印count的值。

示例: 这个示例展示了上述方法的使用。

<script>
    // function that count character occurrences in string 
    function count_occur( str )
    {
      // checking string is valid or not 
      if( str.length == 0 )
      {
        console.log("Invalid string")
        return;
      }
      //cor loop to iterate over string
      for( let i = 0 ;i < str.length ;i++)
      { 
        //variable counting occurrence 
        let count = 0;
        for( let j = 0 ;j < str.length ;j++)
        {
          if( str[i] == str[j] && i > j  )
          {
           break;
          }
          if( str[i] == str[j]  )
          {
            count++;
          }
        }
        if( count > 0)
        console.log(`{str[i]} occurs{count} times`);
         
      }
     
    }
     
    // test string
    let test_str = "gfghello";
    count_occur( test_str);
</script>

输出:

g occurs 2 times
f occurs 1 times
h occurs 1 times
e occurs 1 times
l occurs 2 times
o occurs 1 times

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程