LeetCode 1287. Element Appearing

2020-12-04  本文已影响0人  LiNGYu_NiverSe

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:

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:

上一篇 下一篇

猜你喜欢

热点阅读