Home  >  Article  >  Web Front-end  >  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:365248browse

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;i++){
    if(!hash[arr[i]])hash[arr[i]]=1;//没有初始化的数组元素为undefined,undefined++为NaN
    else hash[arr[i]]++;
  }
  var max=0;//最多的次数
  var maxV;//出现最多的元素
  var second=0;//第二多的次数
  var secondV;//出现第二多的元素
  hash.forEach(function(item,index){//forEach函数会跳过空元素
    if(item>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);

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;i++){
    if(arr[i]==arr[i-1]){
      temp[temp.length-1].index++;
    }else{//不相同则新增一个对象元素
      temp.push({index:1,value:arr[i]});
    }
  }
  temp.sort(function(a,b){//按照出现次数从大到小排列
    return a.index<b.index;
  })
  var max=temp[0].index;
  var maxV=temp[0].value;
  var second=temp[1].index;
  var secondV=temp[1].value;
  return {max,maxV,second,secondV};
}
var arr=[2,2,3,4,5,100,100,,3,1,4,4,100,100];
var {max,maxV,second,secondV}=f(arr);
console.log(max,maxV,second,secondV);

Running results:

This method can not only be used for statistics of integer arrays, but also for statistics of character arrays

The above code is written in the form of ES6

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;i++){
    if(arr[i]==arr[i-1]){
      temp[temp.length-1].add();
    }else{
      temp.push(new num(arr[i]));
    }
  }
  temp.sort(function(a,b){
    return a.index<b.index;
  })
  let max=temp[0].index;
  let maxV=temp[0].value;
  let second=temp[1].index;
  let secondV=temp[1].value;
  return {max,maxV,second,secondV};
}
var arr=['a','b','a','b','a','c','d','d','d','d'];
var {max,maxV,second,secondV}=f(arr);
console.log(max,maxV,second,secondV);

Running results:

Related recommendations:

js Get any non-repeating random array elements in an array [Original]_javascript skills

js Get the last element of the array_javascript skills

JS method to get the maximum value, minimum value and length of the array_javascript skills

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