Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

169. Majority Element

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
2
Input: [3,2,3]
Output: 3

Example 2:

1
2
Input: [2,2,1,1,1,2,2]
Output: 2

找大多數值,一開始會想用個字典統計出現次數,但發現可以用set分出單個數字再統計出現次數,最後取得最大值,比之前想法簡便很多,學習了。


如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels