Easy java solution


  • 0
    J

    public List<Integer> majorityElement(int[] nums) {
    List<Integer> list= new ArrayList<>();
    HashMap<Integer,Integer> map = new HashMap<>();
    if(nums.length<1){
    return list;
    }
    int n = nums.length;
    for(int i=0;i<n;i++){
    if(map.containsKey(nums[i])){
    map.put(nums[i],map.get(nums[i])+1);
    }
    else{
    map.put(nums[i],1);
    }
    if(map.getOrDefault(nums[i],0)>(n/3)){
    list.add(nums[i]);
    map.put(nums[i],-n);
    }
    }
    return list;
    }


Log in to reply
 

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