YouTube Icon

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.

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

Our mission is "Har Koi Dekhe Video, Har Ghar Dekhe Video, Ghar Ghar Dekhe Video" so we Provide videos related to Tutorials, Travel, Technology, Wedding, Cooking, Dance, Festivals, Celebration.

Apna Video Wala
CFT

News & Blogs

4199c21ae5f0296764a6397c7de7f6e2.png

An In-Depth Guide On Choosing Best Frontend Fra...

Since the outbreak of COVID-19, business operations have been moved online to make ends meet....

58f59261d72ce612f18b6a7c527ad4d8.jpg

What is the Difference Between =, ==, and === i...

What is = in JS?  Equivalent to (=) is a task administrator, which sets the variable on t...

417e9598e1b8bdba442a9bac4874f919.jpg

JavaScript Mobile Frameworks worth considering ...

When it comes to startups entering the business arena, they have very tough competition. This is ...

Top Authors

Lamia Rochdi is the Marketing Manager at Bell Flavors & Fragrances EMEA. A successful family-...

Lamia Rochdi

I’m Mertin Wilson a technician in a camera company and certified expert of different P...

Mertin Wilson

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

Zakariya Usman

Pankaj Singh is a Senior Digital Marketing Consultant with more than 2 years of experience in SEO...

Pankaj Singh
CFT

Our Client Says

WhatsApp Chat with Our Support Team