Home >Web Front-end >JS Tutorial >Detailed explanation of JS method to get the most frequent elements in an array

Detailed explanation of JS method to get the most frequent elements in an array

小云云
小云云Original
2018-01-20 10:15:365438browse

This article mainly introduces the method of JS to obtain the most frequent and second most frequent elements in an array, involving JavaScript's traversal, sorting, judgment, query and other related operation skills for arrays. Friends who need it can refer to it. I hope it can help. Everyone.

The elements with the most and second most occurrences in the integer array

Use the hash array

function f(arr){
  var i;
  var length=arr.length;
  var hash=[];//使用哈希数组
  for(i=0;i<length>max){
      second=max;
      secondV=maxV;
      max=item;
      maxV=index;//用索引来保存原数组的值
    }else if(item>second){
      second=item;
      secondV=index;
    }
  });
  return {max,maxV,second,secondV};
}
var arr=[2,2,2,2,3,4,5,4,3,1,4,4,100,100];
var {max,maxV,second,secondV}=f(arr);//ES的元素解构赋值
console.log(max,maxV,second,secondV);</length>

Running result:

The elements with the most and second most occurrences in the array

Use objects to save values ​​and times

function f(arr){
  var temp=[];//对象数组
  var i;
  temp[0]={value:arr[0],index:1};//保存数组元素出现的次数和值
  arr.sort();
  for(i=1;i<arr.length var console.log><p>Running results:</p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/054/025/20a1ea4e9347cd286b0b883813818c60-1.jpg?x-oss-process=image/resize,p_40" class="lazy" alt=""></p>
<p>This method can not only be used for statistics of integer arrays, but also for statistics of character arrays</p>
<p>The above code is written in the form of ES6</p>
<pre class="brush:php;toolbar:false">function f(arr){
  class num{
    constructor(value){
      this.value=value;
      this.index=1;
    }
    add(){
      this.index++;
    }
  }
  arr.sort();
  let temp=[];
  temp[0]=new num(arr[0]);
  for(let i=1;i<arr.length var console.log><p>Running results:</p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/054/025/20a1ea4e9347cd286b0b883813818c60-2.jpg?x-oss-process=image/resize,p_40" class="lazy" alt=""></p>
<p>Related recommendations: </p>
<p><a href="http://www.php.cn/js-tutorial-22408.html" target="_self">js Get any non-repeating random array elements in an array [Original]_javascript skills</a></p>
<p><a href="http://www.php.cn/js-tutorial-7927.html" target="_self">js Get the last element of the array_javascript skills</a></p>
<p><a href="http://www.php.cn/js-tutorial-4095.html" target="_self">JS method to get the maximum value, minimum value and length of the array_javascript skills</a></p></arr.length>

The above is the detailed content of Detailed explanation of JS method to get the most frequent elements in an array. 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