# 多数元素 LeeCode --- 给定一个大小为`n`的数组,找到其中的多数元素。多数元素是指在数组中出现次数大于`⌊ n/2 ⌋`的元素。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 ```shell= 示例1: 输入: [3,2,3] 输出: 3 ``` ```shell= 示例2: 输入: [2,2,1,1,1,2,2] 输出: 2 ``` ```golang= func majorityElement(nums []int) int { m := make(map[int]int) for i := 0; i < len(nums); i++ { num := nums[i] m[num] = m[num] + 1 if m[num] > len(nums)/2 { return num } } return 0 } ``` 题目来源 转载:https://leetcode-cn.com/problems/majority-element 来源:力扣(LeetCode) ###### tags: `LeeCode`