Majority Element Problem Statement

Given an array/list 'ARR' consisting of 'N' integers, your task is to find the majority element in the array. If there is no majority element present, return -1.

Example:

Input:
T = 2
N = 3
ARR = [3, 3, 4]
N = 5
ARR = [1, 1, 2, 2, 2]
Output:
3
2
Explanation:

For the first test case, majority element is 3 as it appears more than floor(3/2) = 1 times. For the second, 2 is the majority element as it appears more than floor(5/2) = 2 times.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5 * 103
  • -105 <= ARR[i] <= 105

Time limit: 1 sec

Note:

A majority element is an element that occurs more than floor(N / 2) times in the array. The output is managed by the system; you only need to implement the function to find the majority element.

AnswerBot
1d

Find the majority element in an array, return -1 if no majority element is present.

  • Iterate through the array and keep track of the count of each element using a hashmap.

  • Check if any element's count is...read more

Help your peers!
Add answer anonymously...
Rupeek Android Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter