Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
Example 1:
1 | Input: [3,2,3] |
Example 2:
1 | Input: [2,2,1,1,1,2,2] |
找大多數值,一開始會想用個字典統計出現次數,但發現可以用set分出單個數字再統計出現次數,最後取得最大值,比之前想法簡便很多,學習了。
如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)