用hashmap装载,可以符合o(n)的时间复杂度

import java.util.*;
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        HashMap<Integer,Integer> h = new HashMap<>();
        int max = 0;
        int index = 0;
        if(array.length<2){
            return array[0];
        }
        for(int i = 0; i<array.length;i++){
            if(h.containsKey(array[i])){
                int flag = h.get(array[i]);
                flag++;
                h.put(array[i],flag);
                if(flag > max){
                    index = array[i];
                    max = flag;
                }
            }
            else{
                h.put(array[i],0);
            }
        }
        return index;
    }
}