Superslow java solution


  • 0
    P

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

        return nums[0];
    }
    

    }


Log in to reply
 

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