Home  >  Article  >  Web Front-end  >  js array deduplication and how to determine the most frequent characters in a string

js array deduplication and how to determine the most frequent characters in a string

零到壹度
零到壹度Original
2018-04-12 14:25:332134browse

The content of this article is to share with you the code sharing of s array deduplication and how to count the characters that appear most in a string. It has a certain reference value. Friends in need can refer to it

Array deduplication:

<!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>

Find the string that appears the most:

<!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(){
		$(&#39;.btn&#39;).click(function(){
            let num = $(&#39;.num&#39;).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 = &#39;&#39;;
           	for(var i in json){
           		  if (json[i]> max){
           		  	   max = json [i];
           		  	   word = i;
           		  	}
           		}
            console.log(&#39;the word is&#39;+word,&#39;count is&#39;+max);
            })
          })
  </script>
	</head>
	<body>
<label>awqewqewqewqe</label><input type="text" class="num"></input>
	<button class="btn" >22222</button>
</body>
</html>

Related recommendations:

js implements finding the character with the most occurrences in a string and counting its occurrences

Determine the character with the most occurrences and the number of occurrences in a string

js Reorganize string slices

The above is the detailed content of js array deduplication and how to determine the most frequent characters in a string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn