Skip to main content

TCS

TCS OA-20 2024

Problem Description

You are given an array of numbers, and your job is to find the majority element. The majority element is the one that shows up more than half the time in the array.

Input Format

  • The first line contains an integer N, representing the array's length.
  • The second line contains N space-separated integers.

Output Format

Print the element is the one that shows up more than half the time in the array.

Constraints

  • 1 ≤ arr.size() ≤ 10⁴
  • -10³ ≤ arr[i] ≤ 10³

Example

Sample Input:

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

Sample Output:

2

Explanation:

In this array, 2 appears four times, and 1 appears three times. Since 2 appears more than half the time (which is 4 out of 7 elements), it is the majority element.