Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write a function to calculate the majority element in an array (an element that appears more than n/2 times).

Hard

Problem Statement

Write a function to calculate the majority element in an array (an element that appears more than n/2 times).

Sample Input

[2, 2, 1, 1, 1, 2, 2]

Sample Output

Majority Element: 2

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
function majorityElement(arr: number[]): number { // Your logic here }
Log in or sign up to run or submit your code.Log inSign up
Input
[2, 2, 1, 1, 1, 2, 2]
Expected Output
Majority Element: 2