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.
给定一个以非降序排序的整数数组,该数组中恰好有一个整数出现超过25%。
Return that integer.
返回该整数。
Example 1:
Input: arr = [1,2,2,6,6,6,6,7,10]
Output: 6
Constraints:
- 1 <= arr.length <= 10^4
- 0 <= arr[i] <= 10^5
Solution:
# solution 1
from collections import Counter
class Solution:
def findSpecialInteger(self, arr: List[int]) -> int:
return Counter(arr).most_common(1)[0][0]
# solution 2
class Solution:
def findSpecialInteger(self, arr: List[int]) -> int:
return max(set(arr), key = a.count)
Both solution: