JS Array de-weight, and statistics up to the algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.