https://leetcode-cn.com/problems/majority-element/
很简单
1.可以先排序,返回中间的元素
2.利用map
class Solution {
public int majorityElement(int[] nums) {
int res = nums[0];
Map<Integer,Integer> map = new HashMap<>();
for(int i=0; i<nums.length; i++){
if(map.containsKey(nums[i])){
map.put(nums[i], map.get(nums[i])+1);
}else{
map.put(nums[i], 1);
}
}
for(Integer key: map.keySet()){
if((map.get(key))>(nums.length/2)){
res = key;
}
}
return res;
}
}