8. Majority Element

8. Majority Element

Easy
58.0%
1.8k
arrayhash-tablegreedy
Google, Amazon, Netflix, Meta (Facebook), Apple, Microsoft, Uber, Bloomberg, Airbnb, Stripe, Adobe, Salesforce, LinkedIn, Oracle

You’re interviewing for a backend or data engineering role at a FAANG-level company. The interviewer describes a real product scenario:

A large-scale recommendation or analytics platform processes user-generated events in real time. For certain business decisions, the platform needs to quickly determine the dominant signal in a batch of data.

Each signal is represented as an integer, and the platform guarantees that one signal always appears more than half of the time in the dataset. This dominant signal is called the majority element.

The data is stored in an integer array nums, and due to performance constraints, the platform needs a solution that works efficiently even for very large datasets.

This is a common coding interview / DSA question might ask in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.

Your Task

Given an integer 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.

Input Format

  • nums: an array of integers

Output Format

  • A single integer representing the majority element

Examples

Example 1:

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

Example 2:

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

Constraints

  • 1 <= nums.length <= 100,000
  • -10^9 <= nums[i] <= 10^9
  • A majority element always exists

Loading editor...

[3,2,3]
3

Console Output

Click "Run Code" or "Submit" to see results

Your code will be evaluated by AI with instant feedback