Skip to content
Katabench
Try free

Majority Element

Easy About 10 min Free, no account needed

Find the element that appears more than half the time.

The brief

Given an array of integers nums, return the majority element, the value that appears more than n / 2 times.

  • A majority element always exists in the input.

The performance bar

The obvious approach counts every element's occurrences with a nested scan, O(n2). The hidden test runs on 200,001 elements, where forty billion comparisons are far too slow. You will need to find it in O(n).

Starter code

C#
using System;

public class Solution
{
    // TODO: return the value appearing more than n/2 times (guaranteed to exist).
    public int MajorityElement(int[] nums)
    {
        throw new NotImplementedException();
    }
}

How it is graded

Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.

C# coding challenges →

What it practices

Hash maps and sets
Trade memory for constant-time lookups, membership tests and frequency counts.

Related katas