169. Majority Element

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function majorityElement($nums) {
        $table = [];
        $target = 0;
        foreach($nums as $num) {
            $table[$num] ??= 0;
            $table[$num]++;
            if ($table[$num] > $table[$target]){
                $target = $num;    
            }
        }

        return $target;
    }
}

Last updated