Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear

Stuck with a problem? Got Error? Ask AI support!

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([]);
Copy Clear