Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?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([]);