Leetcode 150 | Day 4: Majority Element - Naive vs. Optimized (opens in new tab)
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