https://leetcode-cn.com/problems/majority-element/
    很简单
    1.可以先排序,返回中间的元素
    2.利用map

    1. class Solution {
    2. public int majorityElement(int[] nums) {
    3. int res = nums[0];
    4. Map<Integer,Integer> map = new HashMap<>();
    5. for(int i=0; i<nums.length; i++){
    6. if(map.containsKey(nums[i])){
    7. map.put(nums[i], map.get(nums[i])+1);
    8. }else{
    9. map.put(nums[i], 1);
    10. }
    11. }
    12. for(Integer key: map.keySet()){
    13. if((map.get(key))>(nums.length/2)){
    14. res = key;
    15. }
    16. }
    17. return res;
    18. }
    19. }