【javascript】查找字符串中显示次数最多的字符及次数
2012-12-19 10:27
302 查看
昨天群里有人问如何查找字符串中显示次数最多的字符及次数,其实网上有很多种方法可以实现,今天抽空也写了一个。
思路是通过字符分割成数组,而字符的显示次数恰好是数组的长度减一,然后判断字符显示次数的大小。
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>查找字符串中显示次数最多的字符及次数</title> <style type="text/css"> p{font:36px SimSun;line-height:36px;} </style> </head> <body> <p id="showStr"></p> <p id="showCount"></p> </body> </html> <script type="text/javascript"> window.onload = function(){ var oDiv = document.getElementById('showStr'); var oDiv2 = document.getElementById('showCount'); var str = 'lasdjfowqefulasddfjqseasdffe'; var temp = temp2 = ''; var count = 0; for(var i = 0; i < str.length; i++){ temp = str.charAt(i); if((str.split(temp).length - 1) > count){ count = str.split(temp).length - 1; temp2 = temp; } } oDiv.innerHTML = str; oDiv2.innerHTML = '显示次数最多的是 ' + temp2 + ',显示次数是 ' + count + ' 次'; }; </script>
思路是通过字符分割成数组,而字符的显示次数恰好是数组的长度减一,然后判断字符显示次数的大小。
相关文章推荐
- 题目:JavaScript编程题 查找字符串"sdddrtkjsfkkkasjdddj"中出现次数最多的字符和次数。
- javascript查找字符串中出现最多的字符和次数的小例子
- javascript查找字符串中出现最多的字符和次数的小例子
- Javascript 查找字符串中出现最多的字符和出现的次数
- JavaScript-查找字符串中出现最多的字符和个数
- 高效查找一个字符串中,出现次数最多的字符
- 查找字符串中出现次数最多的字符
- JavaScript计算一个字符串最多重复的字符及出现次数
- 查找一个字符串中出现次处最多的那个字符,并输出出现次数
- 查找出字符串中出现最多的字符及其出现的次数
- js实现从字符串中查找出现次数最多的字符的两种解决办法
- JavaScript字符串最多重复的字符及次数
- 查找一个字符串中重复次数最多的字符
- 查找一个字符串里,出现次数最多的字符,时间复杂度O(n)
- 查找出字符串中出现最多的字符及其出现的次数
- js实现查找字符串出现最多的字符和次数
- 查找字符串中出现次数最多的字符
- JS查找字符串中出现次数最多的字符
- JavaScript知识之判断字符串中出现最多的字符及次数
- JavaScript 计算一个字符串中出现次数最多的字符