DEV Community

Leetcode 150 | Day 4: Majority Element - Naive vs. Optimized (opens in new tab)

Discussed on DEV

Leetcode 169 asks us to identify the majority element in a given array. The majority element is defined as the element that appears more than n / 2 times. We are told that we can assume such an element always exists. For both approaches we will use the following value: nums = [2, 4, 2, 2, 4, 4, 4] Approach 1: Naive (Frequency Map) This approach uses a Map to count the frequency of each element in the array. function majorityElement(nums) { const counts = new Map(); for (const num of nums) { c...

Read the original article
Sign in to keep reading the full article.

Keyboard Shortcuts

Navigation

Next / previous post
j/k
Open post
oorEnter
Preview post
v

Post Actions

Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Save / unsave
s

Recommendations

Add interest / feed
Enter
Not interested
x

Go to

Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Discover
gb
Search
/

General

Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help