How to count the occurrence / frequency of array elements in JavaScript




How to count the occurrence / frequency of array elements in JavaScript

<html>
<head>
<title>frequency</title>
<script>
var arr = [1, 1, 2, 19, 2, 19, 4, 5, 2, 5,1, 1, 2, 19, 2, 19, 4, 5, 2, 5,1, 1, 2, 19, 2, 19, 4, 5, 2, 5,1, 1, 2, 19, 2, 19, 4, 5, 2, 5,19, 2, 19, 4, 5, 2, 5,1, 1, 2, 19, 2, 19, 4, 5, 2, 5,1, 1, 2, 19, 2, 19, 4];
var a = [], b = [], prev;

arr.sort();
for ( var i = 0; i < arr.length; i++ ) {
    if ( arr[i] !== prev ) {
        a.push(arr[i]);
        b.push(1);
    } else {
        b[b.length-1]++;
    }
    prev = arr[i];
}
var result = [a, b];
console.log(result);
document.write('[' + result[0] + ']<br>[' + result[1] + ']') 
</script>
</head>
<body>
</body>
</html>

 



Author Biography.

Lokesh Gupta
Lokesh Gupta

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working in the information technology and services industry. I enjoy solving complex problems within budget and deadlines putting my skills on PHP, MySQL, Python, Codeigniter, Yii2, Laravel, AngularJS, ReactJS, NodeJS to best use. Through Knowledge of UML & visual modeling, application architecture design & business process modeling. Successfully delivered various projects, based on different technologies across the globe.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforJobs is an advanced hiring platform based on artificial intelligence, enabling recruiters to hire top talent effortlessly.

CrowdforJobs

CrowdforApps brings to you the well researched list of the most successful and finest App development companies, Web software developers.

CrowdforApps

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.

CrowdforGeeks

CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.

CrowdforThink
CFT

News & Blogs

d7c8b3433a607583dc5bb1bf47f7041f.png

How to Generate a PDF with JavaScript

An extremely regular use case today is that of enabling your clients to download information from...

d432ce51653c4558fc49e6f6ba3ca0c0.jpg

How to Play Audio After Few Seconds or Delay in...

In this instructional exercise, I will demonstrate to you how effortlessly you can play sound fol...

f95b8815ecdfedc8ef0d66ac816d5027.png

How to Create A Countdown Timer with Progress B...

In this instructional exercise, I am will demonstrate to you proper methodologies to make a comme...

Top Authors

Hey, I am Suraj - a full-time blogger and a social media expert currently working on the Growth H...

Suraj Kumar

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working i...

Lokesh Gupta

With good communication and writing skiils, Astha Sharma is a full-time content writer working wi...

Astha Sharma

Zakariya has recently joined the PakWheels team as a Content Marketing Executive, shortly after g...

Zakariya Usman
CFT

Our Client Says

WhatsApp Chat with Our Support Team