Very very easy Java Solution


  • 0
    L

    public int majorityElement(int[] nums) {
    Map<Integer,Integer> map=new HashMap<Integer,Integer>();
    int k;
    if(nums.length==1)
    return nums[0];
    for(int i=0;i<nums.length;i++){
    if(!map.containsKey(nums[i])){
    map.put(nums[i], 1);
    }else{
    k=map.get(nums[i]);
    if(k>=nums.length/2){
    return nums[i];
    }
    map.put(nums[i],++k);
    }
    }
    return -1;
    }


  • 0
    K
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.