PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php function findMajorityElement($inputArray): string|int|null { $inputArray = array_map(fn($el)=>trim($el), $inputArray); $countValues = array_count_values($inputArray); arsort($countValues); $expectedMajorityElement = reset($countValues); if ( empty($expectedMajorityElement) || count($inputArray) / 2 > $expectedMajorityElement ) { exit("Current array doesn't have a majority element."); } return key($countValues); } echo findMajorityElement([]);
Show:  
Copy Clear