多数元素GofuncmajorityElement(nums[]int)int{numMap:make(map[int]int)for_,v:rangenums{n,ok:numMap[v]ifok{numMap[v]n1}else{numMap[v]1}ifnumMap[v]len(nums)/2{returnv}}return-1}解题思路采用map结构保存每个元素出现的次数当找到某个值出现次数超过半数后则返回pythonclassSolution:defmajorityElement(self,nums:List[int])-int:nums.sort()returnnums[len(nums)//2]解题思路排序后有序数组的中位数则为多数元素