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

Programming Exercises

Implement a program to find the majority element (element that appears more than n/2 times) in an array.

Medium

Problem Statement

Implement a program to find the majority element (element that appears more than n/2 times) in an array.

Sample Input

Array: [2, 2, 1, 1, 2]

Sample Output

Majority element: 2

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
fun findMajorityElement(arr: Array<Int>): Int? { // Add your code here to find the majority element } fun main() { val array = arrayOf(2, 2, 1, 1, 2) println("Majority element: ${findMajorityElement(array)}") }
Log in or sign up to run or submit your code.Log inSign up
Input
Array: [2, 2, 1, 1, 2]
Expected Output
Majority element: 2