Arrays January 12 ,2026

Find the Majority Element in an Array 

Problem Statement

Given an array of n elements, find the majority element.

  • A majority element is an element that appears more than n/2 times in the array.
  • If no element occurs more than n/2 times, return no majority element.
  • This problem is commonly asked in interviews and tests understanding of arrays, counting, and hashing techniques.

Example 1

Input:

arr = [3, 3, 4, 2, 3, 3, 5]

Output:

3

Explanation:

  • Array size n = 7
  • n/2 = 3.5 → an element must appear more than 3 times
  • Element 3 appears 4 times → majority element.

Example 2

Input:

arr = [1, 2, 3, 4]

Output:

No majority element

Explanation:

  • Array size n = 4
  • n/2 = 2 → element must appear more than 2 times
  • No element appears more than twice → no majority element.

Why This Problem Is Important

  • Tests array traversal, counting, and hashing skills.
  • Foundation for Boyer-Moore Voting Algorithm (optimal solution).
  • Helps understand frequency-based problems and threshold detection.

Approaches to Solve the Problem

  1. Brute Force (Nested Loops) ✅ S0imple but slow
  2. Hashing / Frequency Map (Efficient) ✅ Most commonly used in interviews
  3. Boyer-Moore Voting Algorithm ✅ Optimal, O(n) time, O(1) space

In this post, we focus on Brute Force and Hashing approaches.

Approach 1: Brute Force

Idea

  1. For each element in the array, count its occurrences by traversing the array.
  2. If any element occurs more than n/2 times, return it.
  3. Otherwise, return no majority element.

Time & Space Complexity

  • Time Complexity: O(n²)
  • Space Complexity: O(1)

C++ Implementation (Brute Force)

#include 
#include 
using namespace std;

int main() {
    vector arr = {3, 3, 4, 2, 3, 3, 5};
    int n = arr.size();
    bool found = false;

    for(int i=0; i n/2) {
            cout << "Majority element: " << arr[i];
            found = true;
            break;
        }
    }

    if(!found) cout << "No majority element";
}

Java Implementation (Brute Force)

public class MajorityElementBrute {
    public static void main(String[] args) {
        int[] arr = {3, 3, 4, 2, 3, 3, 5};
        int n = arr.length;
        boolean found = false;

        for(int i=0; i n/2) {
                System.out.println("Majority element: " + arr[i]);
                found = true;
                break;
            }
        }

        if(!found) System.out.println("No majority element");
    }
}

Approach 2: Using Hashing / Frequency Map

Idea

  1. Traverse the array and maintain a frequency map.
  2. For each element, increment its count.
  3. If any element occurs more than n/2 times, return it.

Time & Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Python Implementation (Hashing)

arr = [3, 3, 4, 2, 3, 3, 5]
n = len(arr)
freq = {}

for num in arr:
    freq[num] = freq.get(num, 0) + 1

majority = None
for num, count in freq.items():
    if count > n//2:
        majority = num
        break

if majority:
    print("Majority element:", majority)
else:
    print("No majority element")

C++ Implementation (Hashing)

#include 
#include 
#include 
using namespace std;

int main() {
    vector arr = {3, 3, 4, 2, 3, 3, 5};
    int n = arr.size();
    unordered_map freq;

    for(int num : arr) freq[num]++;

    bool found = false;
    for(auto it : freq) {
        if(it.second > n/2) {
            cout << "Majority element: " << it.first;
            found = true;
            break;
        }
    }

    if(!found) cout << "No majority element";
}

JavaScript Implementation (Hashing)

let arr = [3, 3, 4, 2, 3, 3, 5];
let freq = {};
let n = arr.length;

arr.forEach(num => freq[num] = (freq[num] || 0) + 1);

let majority = null;
for(let key in freq){
    if(freq[key] > n/2){
        majority = Number(key);
        break;
    }
}

if(majority) console.log("Majority element:", majority);
else console.log("No majority element");
Find the majority element in an array. (Method 2) – Study Algorithms

Approach 3: Boyer-Moore Voting Algorithm

Idea

  • Maintain a candidate element and a count.
  • Traverse the array:
    • If count = 0, set the current element as the candidate.
    • If the current element == candidate, increment count.
    • Else, decrement count.
  • After traversal, the candidate may be the majority element.
  • Verify if the candidate occurs more than n/2 times.

Time & Space Complexity

  • Time Complexity: O(n) → one pass to find candidate + one pass to verify
  • Space Complexity: O(1)

 

Python Implementation

arr = [3, 3, 4, 2, 3, 3, 5]
n = len(arr)

# Step 1: Find candidate
candidate = None
count = 0

for num in arr:
    if count == 0:
        candidate = num
        count = 1
    elif num == candidate:
        count += 1
    else:
        count -= 1

# Step 2: Verify candidate
if arr.count(candidate) > n // 2:
    print("Majority element:", candidate)
else:
    print("No majority element")

C++ Implementation

#include 
#include 
using namespace std;

int main() {
    vector arr = {3, 3, 4, 2, 3, 3, 5};
    int n = arr.size();

    // Step 1: Find candidate
    int candidate = 0, count = 0;
    for(int num : arr){
        if(count == 0){
            candidate = num;
            count = 1;
        } else if(num == candidate) {
            count++;
        } else {
            count--;
        }
    }

    // Step 2: Verify candidate
    count = 0;
    for(int num : arr){
        if(num == candidate) count++;
    }

    if(count > n/2)
        cout << "Majority element: " << candidate;
    else
        cout << "No majority element";
}

Java Implementation

public class MajorityElementBoyerMoore {
    public static void main(String[] args) {
        int[] arr = {3, 3, 4, 2, 3, 3, 5};
        int n = arr.length;

        // Step 1: Find candidate
        int candidate = 0, count = 0;
        for(int num : arr){
            if(count == 0){
                candidate = num;
                count = 1;
            } else if(num == candidate){
                count++;
            } else {
                count--;
            }
        }

        // Step 2: Verify candidate
        count = 0;
        for(int num : arr){
            if(num == candidate) count++;
        }

        if(count > n/2)
            System.out.println("Majority element: " + candidate);
        else
            System.out.println("No majority element");
    }
}

C# Implementation

using System;

class Program {
    static void Main() {
        int[] arr = {3, 3, 4, 2, 3, 3, 5};
        int n = arr.Length;

        // Step 1: Find candidate
        int candidate = 0, count = 0;
        foreach(int num in arr){
            if(count == 0){
                candidate = num;
                count = 1;
            } else if(num == candidate){
                count++;
            } else {
                count--;
            }
        }

        // Step 2: Verify candidate
        count = 0;
        foreach(int num in arr){
            if(num == candidate) count++;
        }

        if(count > n/2)
            Console.WriteLine("Majority element: " + candidate);
        else
            Console.WriteLine("No majority element");
    }
}

JavaScript Implementation

let arr = [3, 3, 4, 2, 3, 3, 5];
let n = arr.length;

// Step 1: Find candidate
let candidate = null;
let count = 0;

for(let num of arr){
    if(count === 0){
        candidate = num;
        count = 1;
    } else if(num === candidate){
        count++;
    } else {
        count--;
    }
}

// Step 2: Verify candidate
count = arr.filter(x => x === candidate).length;

if(count > Math.floor(n/2))
    console.log("Majority element:", candidate);
else
    console.log("No majority element");

 Summary of Approaches for Majority Element

ApproachTime ComplexitySpace ComplexityNotes
Brute ForceO(n²)O(1)Simple, inefficient
Hashing / Frequency MapO(n)O(n)Common in interviews
Boyer-Moore VotingO(n)O(1)Optimal solution, widely asked in interviews

Next Problem in the Series

Find All Unique Elements

Sanjiv
0

You must logged in to post comments.

Related Blogs

Find the S...
Arrays February 02 ,2026

Find the Second Smal...

Find the S...
Arrays January 01 ,2026

Find the Sum of All...

Find the M...
Arrays January 01 ,2026

Find the Maximum Ele...

Find the M...
Arrays January 01 ,2026

Find the Minimum Ele...

Count Even...
Arrays January 01 ,2026

Count Even and Odd N...

Search an...
Arrays January 01 ,2026

Search an Element in...

Copy One A...
Arrays January 01 ,2026

Copy One Array into...

Reverse an...
Arrays January 01 ,2026

Reverse an Array

Print Alte...
Arrays January 01 ,2026

Print Alternate Elem...

Find the L...
Arrays January 01 ,2026

Find the Length of a...

Check if a...
Arrays January 01 ,2026

Check if an Array is...

Find the F...
Arrays January 01 ,2026

Find the First Eleme...

Find the L...
Arrays January 01 ,2026

Find the Last Elemen...

Count the...
Arrays January 01 ,2026

Count the Number of...

Replace Al...
Arrays January 01 ,2026

Replace All Elements...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at E...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at O...

Find the A...
Arrays January 01 ,2026

Find the Average of...

Count the...
Arrays January 01 ,2026

Count the Number of...

Remove Dup...
Arrays January 01 ,2026

Remove Duplicate Ele...

Move All Z...
Arrays January 01 ,2026

Move All Zeros to th...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by K...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by O...

Check if T...
Arrays January 01 ,2026

Check if Two Arrays...

Merge Two...
Arrays January 01 ,2026

Merge Two Sorted Arr...

Find Missi...
Arrays January 01 ,2026

Find Missing Number...

Find Dupli...
Arrays January 01 ,2026

Find Duplicate Eleme...

Count Freq...
Arrays January 01 ,2026

Count Frequency of E...

Find All U...
Arrays January 01 ,2026

Find All Unique Elem...

Insert an...
Arrays January 01 ,2026

Insert an Element at...

Delete an...
Arrays January 01 ,2026

Delete an Element fr...

Find the I...
Arrays January 01 ,2026

Find the Index of an...

Find Union...
Arrays January 01 ,2026

Find Union of Two Ar...

Find Inter...
Arrays January 01 ,2026

Find Intersection of...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s...

Find the L...
Arrays January 01 ,2026

Find the Largest Sum...

Kadane’s A...
Arrays January 01 ,2026

Kadane’s Algorithm (...

Two Sum Pr...
Arrays January 01 ,2026

Two Sum Problem

Subarray w...
Arrays January 01 ,2026

Subarray with Given...

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Rearrange...
Arrays January 01 ,2026

Rearrange Array Alte...

Leaders in...
Arrays January 01 ,2026

Leaders in an Array

Equilibriu...
Arrays January 01 ,2026

Equilibrium Index of...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s,...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All P...
Arrays January 01 ,2026

Find All Pairs with...

Longest Co...
Arrays January 01 ,2026

Longest Consecutive...

Product of...
Arrays January 01 ,2026

Product of Array Exc...

Maximum Pr...
Arrays January 01 ,2026

Maximum Product Suba...

Find the F...
Arrays January 01 ,2026

Find the First Missi...

Count Inve...
Arrays January 01 ,2026

Count Inversions in...

Rearrange...
Arrays January 01 ,2026

Rearrange Array by S...

Check if A...
Arrays January 01 ,2026

Check if Array Can B...

Trapping R...
Arrays January 01 ,2026

Trapping Rain Water

Find Minim...
Arrays January 01 ,2026

Find Minimum in Rota...

Search in...
Arrays January 01 ,2026

Search in Rotated So...

Median of...
Arrays January 01 ,2026

Median of Two Sorted...

Merge Inte...
Arrays January 01 ,2026

Merge Intervals

Count Reve...
Arrays January 01 ,2026

Count Reverse Pairs

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Largest Re...
Arrays January 01 ,2026

Largest Rectangle in...

Maximum Su...
Arrays January 01 ,2026

Maximum Sum Rectangl...

Subarray S...
Arrays January 01 ,2026

Subarray Sum Equals...

Count Dist...
Arrays January 01 ,2026

Count Distinct Eleme...

Sliding Wi...
Arrays January 01 ,2026

Sliding Window Maxim...

Find K Max...
Arrays January 01 ,2026

Find K Maximum Eleme...

Minimum Nu...
Arrays January 01 ,2026

Minimum Number of Ju...

Chocolate...
Arrays January 01 ,2026

Chocolate Distributi...

Find All T...
Arrays January 01 ,2026

Find All Triplets Wi...

Kth Smalle...
Arrays January 01 ,2026

Kth Smallest Element...

Maximum Le...
Arrays January 01 ,2026

Maximum Length Biton...

Find the S...
Arrays February 02 ,2026

Find the Second Larg...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com