Skip to content
JsDevLife
Menu
  • Home
  • Leetcode in JS
  • About me
  • Contact Us
Menu

Solution to LeetCode Problem 169. Majority Element in JavaScript

Posted on December 18, 2022July 30, 2023 by Vikas Kad

A Solution to LeetCode Problem 169. Majority Element in JavaScript

 

The majority element is the element that appears more than half of the time in a given array. For example, in the array [3, 2, 3], the majority element is 3 as it appears 2 out of 3 times, which is more than half. In this post, we will be discussing a solution to the LeetCode problem #169: Majority Element, using JavaScript.

  • Problem Statement:

  • Given an array nums of size n, return the majority element.


  • The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

    Example 1:

    Input: nums = [3,2,3]
    Output: 3
    

    Example 2:

    Input: nums = [2,2,1,1,1,2,2]
    Output: 2
    

    Constraints:

    • n == nums.length
    • 1 <= n <= 5 * 104
    • -109 <= nums[i] <= 109

     

Solution:

One way to solve this problem is to use a brute force approach, where we can iterate through the array and count the number of occurrences of each element. If the count of an element is greater than n/2, then we return that element as the majority element.

 

However, this solution has a time complexity of O(n^2), which is not very efficient.

 

A better solution would be to use a hash table to store the count of each element as we iterate through the array. This way, we can find the majority element in one pass, with a time complexity of O(n).

 

Here is the implementation of this solution in JavaScript:

/**
 * @param {number[]} nums
 * @return {number}
 */
const majorityElement = function(nums) {
    let hash = {};
    for (let i = 0; i < nums.length; i++) {
        if (hash[nums[i]]) {
            hash[nums[i]]++;
        } else {
            hash[nums[i]] = 1;
        }
    }
    for (let key in hash) {
        if (hash[key] > nums.length/2) {
            return key;
        }
    }
};

 

We first create an empty hash table called hash. Then, we iterate through the nums array and store the count of each element in the hash table. Finally, we iterate through the hash table and return the key (i.e., the element) if its count is greater than n/2.

 

This solution has a time complexity of O(n) and a space complexity of O(n), as we are using a hash table to store the counts of each element.

 

Conclusion:

 

In this post, we discussed a solution to the LeetCode problem #169: Majority Element, using JavaScript. We used a hash table to store the count of each element and found the majority element in one pass, with a time complexity of O(n).

 

I hope this post was helpful in explaining the solution to this problem. If you have any questions or suggestions, please leave a comment below.

 

 

 

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Solving LeetCode Problem 79 – Word Search in JavaScript
  • Solving LeetCode Problem 48: Rotate Image using JavaScript
  • Solving the “Container With Most Water” LeetCode Problem in JavaScript – A Comprehensive Guide
  • LeetCode Solution: 54: Spiral Matrix in JavaScript
  • Solution to LeetCode Problem 31. Next Permutation in JavaScript

Archives

  • 2023 (5)
  • 2022 (20)
  • 2021 (2)
  • 2020 (4)
  • 2019 (14)
  • 2018 (17)

Categories

  • blockchain development
  • Blog
  • crystal
  • flutter
  • flutter.io
  • GitHub
  • Installation
  • Ionic Framework
  • javascript
  • leetcode-in-js
  • masteringInJavasript
  • mcqs
  • MongoDB
  • nodejs
  • Object Oriented Javacript
  • python
  • smart contracts
  • visual studio

Quick Links

  • Home
  • Leetcode in JS
  • About me
  • Contact Us

Terms of service

  • Terms Of Service
  • Disclaimer
©2023 JsDevLife | Design: Newspaperly WordPress Theme