Given an array, pass it into a highestRank (arr) function, and return the element with the highest frequency in the array. Given an array, pass it into a highestRank (arr) function, and return the element with the highest frequency in the array.
If there are multiple elements with the maximum frequency, the element with the maximum number is returned.
Example:
arr = [12, 10, 8, 12, 7, 6, 4, 10, 12]; highestRank(arr) //=> returns 12 arr = [12, 10, 8, 12, 7, 6, 4, 10, 12, 10]; highestRank(arr) //=> returns 12 arr = [12, 10, 8, 8, 3, 3, 3, 3, 2, 4, 10, 12, 10]; highestRank(arr) //=> returns 3
For this kind of appearance frequency, we 'd better make a statistical statement to see how often each number appears.
Then, select the one with the most frequent appearance, or the number of groups.
Finally, find the largest number from the number and return it.
I personally prefer to use hash objects for efficient and easy enumeration.
Sorting can also be solved, but the efficiency is certainly lower.
function highestRank(arr){ var hash = {}; var highest = 0; var highestArray = []; for(var i=0;i highest){ highest = hash[cur]; } } for(var j in hash){ if(hash.hasOwnProperty(j)){ if(hash[j] === highest){ highestArray.push(j); } } } return Math.max.apply(null,highestArray); }
The above is JavaScript fun: Find the content of the element with the highest frequency of appearance in the array. For more information, see the PHP Chinese Network (www.php1.cn )!