Engaged for a period of time the front-end development, today wrote an array to go heavy, and to count the most items of the method, currently only supported array of items are numbers.
Due to my limited ability, I hope to get the Netizen's correction! If there is a problem or better to realize the idea, also welcome everyone to discuss with me! The code is as follows:
function Getmax (arr) {
var obj = {},
L = arr.length,
Key = [],
val = [];
for (var i = 0;i<l;i++) {
!obj[arr[i]]? Obj[arr[i]] = 1:obj[arr[i]] + = 1;
}
for (i in obj) {
Key.push (i);
Val.push (Obj[i]);
}
var max = Math.max.apply ("", Val);
var pos = (function () {
var j, len = Val.length;
for (j=0;j<len;j++) {
if (max = = Val[j]) {
Return J;
}
}
})();
Console.log ("Most occurrences are:" +key[pos]+ ", the number of occurrences is:" +val[pos])
}
var arr = [1,2,3,4,4,1,1];
Getmax (arr);
The test results are as follows:
JS Array de-weight, and statistics up to the algorithm