Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

6. Majority Element

EasyDay 2 of 60Arrays & Hashing

Given an array nums of size n, return its majority element: the value that appears more than n / 2 times. You can assume such an element always exists.

Example 1
Input: nums = [3,2,3]
Output: 3
3 appears 2 times, which is more than 3 / 2.
Example 2
Input: nums = [2,2,1,1,1,2,2]
Output: 2
Constraints
  • 1 <= nums.length <= 5 * 10^4
  • -10^9 <= nums[i] <= 10^9
  • A majority element always exists
Hints
Frequently asked at: Amazon, Google, Microsoft, Adobe, TCS
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min