Skip to content
CodeKruPro

Find the majority element

Coding problemEasy-MediumAsked in: Amazon, Atlassian, D. E. Shaw, Flipkart, Google, Microsoft

Given a list of integers, return the value that appears more than half of the time. Such a value is always present in the tests.

Try to use only a constant amount of extra memory. The largest hidden tests have 100,000 values.

Examples

Example 1
Input: nums = [3,2,3]
Output: 3
Example 2
Input: nums = [2,2,1,1,1,2,2]
Output: 2
Example 3
Input: nums = [5]
Output: 5

Constraints

  • The list holds between 1 and 100,000 integers
  • Each value is between -1,000,000,000 and 1,000,000,000
  • One value appears more than half of the time

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in