โ— LIVE
OpenAI releases GPT-5 APIIndia AI startup raises $120MBitcoin ETF hits record inflowsMeta Llama 4 benchmarks leakedOpenAI releases GPT-5 APIIndia AI startup raises $120MBitcoin ETF hits record inflowsMeta Llama 4 benchmarks leaked
๐Ÿ“… Sat, 21 Mar, 2026โœˆ๏ธ Telegram
AiFeed24

AI & Tech News

๐Ÿ”
โœˆ๏ธ Follow
๐Ÿ Home๐Ÿค–AI๐Ÿ’ปTech๐Ÿš€Startupsโ‚ฟCrypto๐Ÿ”’Security๐Ÿ‡ฎ๐Ÿ‡ณIndiaโ˜๏ธCloud๐Ÿ”ฅDeals
โœˆ๏ธ News Channel๐Ÿ›’ Deals Channel
Home/Cloud & DevOps/Majority Element
โ˜๏ธCloud & DevOps

Majority Element

Introduction Finding the most frequent element in an array might seem simple, but doing it efficiently is the real challenge. This problem introduces a powerful technique called the Boyer-Moore Voting Algorithm, which solves it in linear time and constant space. Given an array arr, find the majority

โšกQuick SummaryAI generating...
C

Christina Sharon S

๐Ÿ“… Mar 21, 2026ยทโฑ 3 min readยทDev.to โ†—
โœˆ๏ธ Telegram๐• TweetWhatsApp
๐Ÿ“ก

Original Source

Dev.to

https://dev.to/christina_sharons_2b3205/majority-element-4eff
Read Full โ†—

Introduction

Finding the most frequent element in an array might seem simple, but doing it efficiently is the real challenge.

This problem introduces a powerful technique called the Boyer-Moore Voting Algorithm, which solves it in linear time and constant space.

Problem Statement

Given an array arr, find the majority element.

A majority element is one that appears more than n/2 times.

  • If it exists โ†’ return the element
  • If not โ†’ return -1

Example 1

Input:

```python id="z1mqz9"
arr = [1, 1, 2, 1, 3, 5, 1]




Output:



```python id="sdif4r"
1

Example 2

Input:

```python id="q3m2zx"
arr = [7]




Output:



```python id="d9q2k3"
7

Example 3

Input:

```python id="v2g9bn"
arr = [2, 13]




Output:



```python id="l0w7fp"
-1

Brute Force Approach

  • Count frequency of each element
  • Return element with count > n/2

Time Complexity: O(nยฒ)

Better Approach (Hash Map)

  • Use a dictionary to count frequencies

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

Optimal Approach: Boyer-Moore Voting Algorithm

Key Idea

  • Maintain:

    • candidate
    • count
  • If count becomes 0 โ†’ pick new candidate

  • Increase count if same element

  • Decrease count if different

Why It Works

The majority element appears more than half the time, so it cannot be canceled out by other elements.

Python Implementation

```python id="kl8r8o"
def majority_element(arr):
candidate = None
count = 0

# Step 1: Find candidate
for num in arr:
    if count == 0:
        candidate = num

    if num == candidate:
        count += 1
    else:
        count -= 1

# Step 2: Verify candidate
if arr.count(candidate) > len(arr) // 2:
    return candidate

return -1

Example usage

arr = [1, 1, 2, 1, 3, 5, 1]
print(majority_element(arr))




---

## Step-by-Step Example

For:



```python id="wz0l9h"
[1, 1, 2, 1, 3, 5, 1]
  • Start with candidate = 1
  • Matching values increase count
  • Different values decrease count
  • Final candidate remains 1

Key Points

  • No extra space required
  • Works in a single pass
  • Must verify candidate at the end
  • Very important interview algorithm

Conclusion

The Majority Element problem demonstrates how smart observation can lead to highly efficient algorithms. The Boyer-Moore Voting Algorithm is a must-know technique for coding interviews and competitive programming.

Understanding this approach helps you solve many similar problems involving frequency and dominance in arrays.

Tags:#cloud#dev.to

Found this useful? Share it!

โœˆ๏ธ Telegram๐• TweetWhatsApp

Read the Full Story

Continue reading on Dev.to

Visit Dev.to โ†—

Related Stories

โ˜๏ธ
โ˜๏ธCloud & DevOps

Building a SQL Tokenizer and Formatter From Scratch โ€” Supporting 6 Dialects

about 3 hours ago

โ˜๏ธ
โ˜๏ธCloud & DevOps

Markdown Knowledge Graph for Humans and Agents

about 3 hours ago

Moving Beyond Disk: How Redis Supercharges Your App Performance
โ˜๏ธCloud & DevOps

Moving Beyond Disk: How Redis Supercharges Your App Performance

about 3 hours ago

The Stake Was Governance Outside the Schema. MICA v0.1.5 Pulled It In
โ˜๏ธCloud & DevOps

The Stake Was Governance Outside the Schema. MICA v0.1.5 Pulled It In

about 3 hours ago

๐Ÿ“ก Source Details

Dev.to

๐Ÿ“… Mar 21, 2026

๐Ÿ• about 3 hours ago

โฑ 3 min read

๐Ÿ—‚ Cloud & DevOps

Read Original โ†—

Web Hosting

๐ŸŒ Hostinger โ€” 80% Off Hosting

Start your website for โ‚น69/mo. Free domain + SSL included.

Claim Deal โ†’

๐Ÿ“ฌ AiFeed24 Daily

Top 5 AI & tech stories every morning. Join 40,000+ readers.

โœฆ 40,218 subscribers ยท No spam, ever

Cloud Hosting

โ˜๏ธ Vultr โ€” $100 Free Credit

Deploy cloud servers in 25+ locations. From $2.50/mo. No contract.

Claim $100 Credit โ†’
AiFeed24

India's AI-powered tech news hub. Daily coverage of AI, startups, crypto and emerging technology.

โœˆ๏ธ๐Ÿ›’

Topics

Artificial IntelligenceStartups & VCCryptocurrencyCybersecurityCloud & DevOpsIndia Tech

Company

About AiFeed24Write For UsContact

Daily Digest

Top 5 AI stories every morning. 40,000+ readers.

No spam, ever.

ยฉ 2026 AiFeed24 Media.Affiliate Disclosure โ€” We earn commission on qualifying purchases at no extra cost to you.
PrivacyTermsCookies