Check if an array has a majority element
Last Updated :
01 Aug, 2022
Given an array, the task is to find if the input array contains a majority element or not. An element is
Examples:
Input : arr[] = {2, 3, 9, 2, 2}
Output : Yes
A majority element 2 is present in arr[]
Input : arr[] = {1, 8, 9, 2, 5}
Output : No
A simple solution is to traverse through the array. For every element, count its occurrences. If the count of occurrence of any element becomes n/2, we return true.
An efficient solution is to use hashing. We count occurrences of all elements. If count becomes n/2 or more return true.
Below is the implementation of the approach.
C++
// Hashing based C++ program to find if there
// is a majority element in input array.
#include <bits/stdc++.h>
using namespace std;
// Returns true if there is a majority element
// in a[]
bool isMajority(int a[], int n)
{
// Insert all elements in a hash table
unordered_map<int, int> mp;
for (int i = 0; i < n; i++)
mp[a[i]]++;
// Check if frequency of any element is
// n/2 or more.
for (auto x : mp)
if (x.second >= n/2)
return true;
return false;
}
// Driver code
int main()
{
int a[] = { 2, 3, 9, 2, 2 };
int n = sizeof(a) / sizeof(a[0]);
if (isMajority(a, n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Hashing based Java program
// to find if there is a
// majority element in input array.
import java.util.*;
import java.lang.*;
import java.io.*;
class Gfg
{
// Returns true if there is a
// majority element in a[]
static boolean isMajority(int a[], int n)
{
// Insert all elements
// in a hash table
HashMap <Integer,Integer> mp = new
HashMap<Integer,Integer>();
for (int i = 0; i < n; i++)
if (mp.containsKey(a[i]))
mp.put(a[i], mp.get(a[i]) + 1);
else mp.put(a[i] , 1);
// Check if frequency of any
// element is n/2 or more.
for (Map.Entry<Integer, Integer> x : mp.entrySet())
if (x.getValue() >= n/2)
return true;
return false;
}
// Driver code
public static void main (String[] args)
{
int a[] = { 2, 3, 9, 2, 2 };
int n = a.length;
if (isMajority(a, n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Ansu Kumari
Python3
# Hashing based Python
# program to find if
# there is a majority
# element in input array.
# Returns true if there
# is a majority element
# in a[]
def isMajority(a):
# Insert all elements
# in a hash table
mp = {}
for i in a:
if i in mp: mp[i] += 1
else: mp[i] = 1
# Check if frequency
# of any element is
# n/2 or more.
for x in mp:
if mp[x] >= len(a)//2:
return True
return False
# Driver code
a = [ 2, 3, 9, 2, 2 ]
print("Yes" if isMajority(a) else "No")
#This code is contributed by Ansu Kumari
C#
// Hashing based C# program
// to find if there is a
// majority element in input array.
using System;
using System.Collections.Generic;
class GFG
{
// Returns true if there is a
// majority element in a[]
static Boolean isMajority(int []a, int n)
{
// Insert all elements
// in a hash table
Dictionary<int,
int> mp = new Dictionary<int,
int>();
for (int i = 0; i < n; i++)
{
if(mp.ContainsKey(a[i]))
{
var val = mp[a[i]];
mp.Remove(a[i]);
mp.Add(a[i], val + 1);
}
else
{
mp.Add(a[i], 1);
}
}
// Check if frequency of any
// element is n/2 or more.
foreach(KeyValuePair<int, int> x in mp)
if (x.Value >= n / 2)
return true;
return false;
}
// Driver code
public static void Main (String[] args)
{
int []a = { 2, 3, 9, 2, 2 };
int n = a.Length;
if (isMajority(a, n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// Hashing based Javascript program to find if there
// is a majority element in input array.
// Returns true if there is a majority element
// in a[]
function isMajority(a, n)
{
// Insert all elements in a hash table
let mp = new Map();
for (let i = 0; i < n; i++){
if(mp.has(a[i])){
mp.set(a[i], mp.get(a[i]) + 1)
}else{
mp.set(a[i], 1)
}
}
// Check if frequency of any element is
// n/2 or more.
for (let x of mp)
if (x[1] >= n/2)
return true;
return false;
}
// Driver code
let a = [ 2, 3, 9, 2, 2 ];
let n = a.length;
if (isMajority(a, n))
document.write("Yes");
else
document.write("No");
// This code is contributed by saurabh_jaiswal.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Check for Majority Element in a sorted array Given an array arr of N elements, A majority element in an array arr of size N is an element that appears more than N/2 times in the array. The task is to write a function say isMajority() that takes an array (arr[] ), arrayâs size (n) and a number to be searched (x) as parameters and returns true i
15+ min read
Majority element in a circular array of 0's and 1's Given a circular array containing only 0âs and 1âs, of size n where n = p*q (p and q are both odd integers). The task is to check if there is a way such that 1 will be in majority after applying the following operations: Divide circular array into p subarrays each of size q.In each subarray, the num
15+ min read
Javascript Program to Check Majority Element in a sorted array Question: Write a function to find if a given integer x appears more than n/2 times in a sorted array of n integers. Basically, we need to write a function say isMajority() that takes an array (arr[] ), arrayâs size (n) and a number to be searched (x) as parameters and returns true if x is a majorit
3 min read
Check whether K times of a element is present in array Given an array arr[] and an integer K, the task is to check whether K times of any element are also present in the array. Examples : Input: arr[] = {10, 14, 8, 13, 5}, K = 2 Output: Yes Explanation: K times of 5 is also present in an array, i.e. 10. Input: arr[] = {7, 8, 5, 9, 11}, K = 3 Output: No
8 min read
Check if an array contains only one distinct element Given an array arr[] of size N, the task is to check if the array contains only one distinct element or not. If it contains only one distinct element then print âYesâ, otherwise print âNoâ. Examples: Input: arr[] = {3, 3, 4, 3, 3} Output: No Explanation: There are 2 distinct elements present in the
8 min read