// Given an array nums of size n, return the majority element. // The majority element is the element that appears more than ⌊n / 2⌋ times. // Example 1: Input: nums = [3,2,3], Output: 3
function majorityElement($nums) {
    $quantity = count($nums);
    $nums = array_count_values($nums);
    foreach($nums as $key => $num) {
        // two values can't be more than 50% of array
	if ($num > $quantity / 2) {
	    return $key;
	}
    }
}
echo majorityElement([0, 1, 1, 3, 4, 1, 1]);   //1
echo majorityElement([2, 2 ,1 ,1 ,1 ,2 ,2]);   //2
by Luka Khitaridze
2 years ago
PHP
Array
0
Pro tip: use ```triple backticks around text``` to write in code fences