Previous Next
0 / 21
Module DEMO-32-106DEMO-C-32

Majority Element

DEMO-C-32 โ€ข Programming in C

Browser-only practice

Problem Statement

Find the majority element in an array. A majority element is an element that appears more than n/2 times. If no majority element exists, print "NONE".

Example

Input:

7 2 2 1 1 1 2 2

Output:

2

(2 appears 4 times, 4 > 3.5)

Constraints

n <= 100

Input Format

n, array

Output Format

Element or NONE

Sample Testcases

Submit runs every public testcase in this browser. Results and code never leave this device.

Sample #1
Public sample
7
2 2 1 1 1 2 2
2
Count is 4.
Sample #2
Public sample
4
1 2 3 4
NONE
No majority.
Sample #3
Public sample
3
1 1 2
1
2 > 1.5.
Sample #4
Public sample
5
3 3 3 2 1
3
3 occurs 3 times, which is > 2.5
Sample #5
Public sample
9
7 7 7 7 7 7 7 7 7
7
All elements are the same
Sample #6
Public sample
11
5 5 5 5 5 6 6 6 6 6 6
6
6 occurs 6 times, which is > 5.5

Web terminal

C, C++, Java, and Python run locally in a browser VM. No worker or visualizer is used.

Saved in this browser
Editor settings