Majority Element
Updated
Problem statement
Given an integer array nums of length n, return its majority element: the value that appears more than n / 2 times.
The input always has a majority element.
Function
majorityElement(nums: int[]) → intExamples
Example 1
nums = [3,2,3]return = 33appears 2 times out of 3, which is more than half.
Example 2
nums = [2,2,1,1,1,2,2]return = 22appears 4 times out of 7 and1only 3 times, so the answer is2.
Example 3
nums = [7,1,7,2,7]return = 77appears 3 times out of 5. The first and last elements are not a reliable answer in general, but here both happen to be7.
Constraints
n == nums.length1 <= n <= 2 × 104-109 <= nums[i] <= 109- Some value appears more than
n / 2times innums.
More ByteDance Phone Screen questions
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
- Binary Tree Target-Sum PathsPHONE SCREEN · Seen Nov 2026
- Longest Increasing SubsequencePHONE SCREEN · Seen Nov 2026
- Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Number of Events That Can Be AttendedPHONE SCREEN · Seen Nov 2026
- Implement Multi-Head AttentionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Meeting RoomsPHONE SCREEN · Seen Oct 2026
- Meeting Rooms IIPHONE SCREEN · Seen Oct 2026
- Shortest-Job-First Process SchedulingPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- Values Exclusive to the Current UserMeta · PHONE SCREENMeta interview questions
- Count Values Appearing Exactly OnceAgoda · PHONE SCREENAgoda interview questions
- Top Two Users by Total Purchase AmountSnowflake · PHONE SCREENSnowflake interview questions
- Restaurant Vote LeadersZipRecruiter · PHONE SCREEN · ONSITE INTERVIEWZipRecruiter interview questions
- AI Role: Collection with Add and Remove OperationsApple · PHONE SCREENApple interview questions
- Employees With Shared InterestsPalantir · PHONE SCREENPalantir interview questions