# 1287. Element Appearing More Than 25% In Sorted Array

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

Example 1:

``````Input: arr = [1,2,2,6,6,6,6,7,10]
Output: 6
``````

Example 2:

``````Input: arr = [1,1]
Output: 1
``````

Constraints:

• `1 <= arr.length <= 10^4`
• `0 <= arr[i] <= 10^5`

``````class Solution {
public:
int findSpecialInteger(vector<int>& arr) {
int n = arr.size(), target = n / 4;
unordered_map<int, int> numCnt;
for (int num : arr) {
if (++numCnt[num] > target) return num;
}
return -1;
}
};
``````

``````class Solution {
public:
int findSpecialInteger(vector<int>& arr) {
int n = arr.size(), t = n / 4;
for (int i = 0; i < n - t; ++i) {
if (arr[i] == arr[i + t]) return arr[i];
}
return -1;
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1287

https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/

https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/discuss/451249/Simple-Java-Solution-O(n)-time-O(1)-space

LeetCode All in One 题目讲解汇总(持续更新中…)

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation