Element Appearing More Than 25% In Sorted Array - Problem

Given an integer array arr sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time.

Return that integer.

Input & Output

Example 1 — Basic Case
$ Input: arr = [1,2,2,6,6,6,6,10]
Output: 6
💡 Note: Array length is 8, so 25% is 2 elements. Element 6 appears 4 times, which is more than 25% (4 > 2), so return 6.
Example 2 — Different Majority Element
$ Input: arr = [1,1,2,2,3,3,3,3,3]
Output: 3
💡 Note: Array length is 9, so 25% is 2.25, meaning we need more than 2 elements. Element 3 appears 5 times (5 > 2.25), so return 3.
Example 3 — Minimum Case
$ Input: arr = [1,1,1,2]
Output: 1
💡 Note: Array length is 4, so 25% is 1 element. Element 1 appears 3 times (3 > 1), so return 1.

Constraints

  • 1 ≤ arr.length ≤ 104
  • -106 ≤ arr[i] ≤ 106
  • arr is sorted in non-decreasing order

Visualization

Tap to expand
Find Element Appearing More Than 25%122666610Input: [1,2,2,6,6,6,6,10]Count: 1: 1 timeCount: 2: 2 timesCount: 6: 4 times ✓Count: 10: 1 timeArray length: 825% threshold: 8/4 = 2Element 6: 4 > 2 ✓Output: 6
Understanding the Visualization
1
Input
Sorted array with one element appearing > 25%
2
Process
Count occurrences and check threshold
3
Output
Return the majority element
Key Takeaway
🎯 Key Insight: In a sorted array, the majority element forms consecutive groups, making counting efficient
Asked in
Google 15 Amazon 12 Microsoft 8
28.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen