Implement a program to find the majority element (element that appears more than n/2 times) in an array.
MediumProblem 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)}")
}
Input
Array: [2, 2, 1, 1, 2]
Expected Output
Majority element: 2