PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

js数组去重以及如何判断出字符串出现次数最多的字符

零到壹度
零到壹度 原创
2018-04-12 14:25:33 1847浏览

本篇文章给大家分享的内容是s数组去重以及如何统计字符串出现最多的字符的代码分享,有着一定的参考价值,有需要的朋友可以参考一下

数组去重:

<!DCOTYPE>
<html>
	<head>
		<title>数组去重</title>
		
		<script>
	function test(str){
		   let len = str.length;
		   let strtmp = [];
		   for(let i = 0;i<len;i++){
		   	   if (strtmp.indexOf(str[i])< 1){
		   	   	   strtmp.push(str[i]);
		   	   	}
		   	}
		 console.log(strtmp);
		}
		let abc = [0,2,3,5,2,1,9,3,9,1];
		test(abc);
  </script>
	</head>
	<body>

</body>
</html>

查找字符串出现最多的:

<!DCOTYPE>
<html>
	<head>
		<title>1111</title>
		<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
		<script>
	$(function(){
		$('.btn').click(function(){
            let num = $('.num').val();
           // console.log(num);
            let len = num.length;
           let json = {};
           for (let i=0;i<len;i++){
           	     if(!json[num.charAt(i)]){
           	     	   json[num.charAt(i)]=1;
           	     	}else
           	     		{
           	     			json[num.charAt(i)]++;
           	     			}
           	}
           	let max = 0;
           	let word = '';
           	for(var i in json){
           		  if (json[i]> max){
           		  	   max = json [i];
           		  	   word = i;
           		  	}
           		}
            console.log('the word is'+word,'count is'+max);
            })
          })
  </script>
	</head>
	<body>
<label>awqewqewqewqe</label><input type="text" class="num"></input>
	<button class="btn" >22222</button>
</body>
</html>

相关推荐:

js实现找出字符串中出现字数最多的字符并计算其出现次数

判断字符串中出现次数最多的字符和出现次数

js 给字符串切片重组

以上就是js数组去重以及如何判断出字符串出现次数最多的字符的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。