题目链接

    使用哈希表记录数字出现次数

    1. var majorityElement = function(nums) {
    2. let map = new Map();
    3. for(let num of nums){
    4. let count;
    5. if(map.has(num)){
    6. count = map.get(num)+1;
    7. map.set(num, count);
    8. }else{
    9. map.set(num,1);
    10. count=1;
    11. }
    12. if(count>nums.length/2){
    13. return num;
    14. }
    15. }
    16. };