iOS Dev India
Learn
Roadmap
Skills
Swift DSA
Build
Studio
Sandbox
Tools
Agent Skills
Community
Communities
Events
Library
Articles
Resources
Jobs
Create Profile
Home
Learn
Roadmap
Skills
Swift DSA
Build
Studio
Sandbox
Tools
Agent Skills
Community
Communities
Events
Library
Articles
Resources
Jobs
Create Profile
Challenges
/
Majority Element
·
Step 31/100 · Dictionaries & Counting
← Character Counts
Most Frequent Value →
Description
Hint
Majority Element
L2
Easy
Arrays & Hashing
Concept
Once you have a frequency dictionary, "which value is common enough" becomes a direct dictionary scan.
Return the integer that appears more than
nums.count / 2
times. If no value is that frequent, return
-1
.
Examples
▸
nums = [1, 2, 3, 2, 2]
→ 2
▸
nums = [1, 2]
→ -1
▸
nums = [1, 1, 1]
→ 1
Progressive Hints
Hint 1 · Nudge
Find out who shows up more than half the time. You can only know that once you know everyone's tally.
Hint 2 · Plan
Tally every value into a frequency map, then return the first key whose count is greater than half the array size. If none qualifies, return -1.
Hint 3 · Approach
counts = frequency map of nums. For each key: if counts[key] > nums.length / 2 return that key. Otherwise return -1.
Reveal the next hint
All hints are out. Take a breath and give it a shot.
Swift
Java
Solution.swift
Tests.swift (Read Only)
Run
Output
// Run your code to see the output here.
Press
ESC
to close.
Start typing to search...