Easiest Way to Find the Most Frequent Element in Array

Easiest Way to Find the Most Frequent Element in Array

The best way to find the most frequent element in an array is using reduce function and creating a hashmap.

First of all, while browsing, I couldn’t find the most effective and short solution to find the most frequent element in an array but I only found a lot of long solutions with for loop however the best way is** not using nested for loop.** Because in Big O Notation, it’s a O(n2) complexity which means it is not effective.

The best way to find the most frequent element in an array is by using reduce function and creating a hashmap. And that’s the whole code you need:

function getMostFrequent(arr) {
   const hashmap = arr.reduce( (acc, val) => {
    acc[val] = (acc[val] || 0 ) + 1
    return acc
 },{})
return Object.keys(hashmap).reduce((a, b) => hashmap[a] > hashmap[b] ? a : b)
}

What I did was, creating a hashmap using reduce. If we have an array like [‘john’, ‘doe’, ’john’, ’bilge’], our hashmap will look like this:

Image for post

Because we create an object using acc in the first reduce function, notice the initial value of acc is {}.

Then we check for each value of the array: is this value already in acc?

If no, put a key-value pair in the object. ( first appearance of the element)

If yes, increment the value of it.

programming javascript coding

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

Who Else Wants to Write Clean JavaScript Code?

Who Else Wants to Write Clean JavaScript Code? 7 Tips to Make Your Coworkers Fall in Love With Your Code.

Code Formatting with Prettier in Visual Studio Code

Formatting code consistently is a pain, especially when working on a team. The beauty of modern-day web development is that the tooling has gotten so much better! In this article, we will look at setting up Prettier to automatically format your code in Visual Studio Code.

Top 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript

Don’t waste time writing long code while you can make it short, yet clearer and easier to read. In this Javascript tutorial, we'll discuss 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript