Find the Majority Element of an Array in PHP
Last Updated :
19 Jun, 2024
We will create a PHP array of integers i.e. $num1 or $num2 and find the element in an array that appears more than n/2 times, where n is the length of the array. The element that appears more than n/2 times will indicate the majority element in the array. These problems in PHP help users solve applications in various domains, such as data analysis, voting systems, and error detection.
These are thefollowing approaches:
Using Sorting
Here, we are going to sort the array and then check the middle element's count. It calculates the length of the array $n and the majority count using the floor($n / 2). It sorts the array in ascending order using the PHP sort() function. If no element in the array satisfies the majority condition, it will return null.
Example: To find the majority element of an array in PHP using sorting method.
PHP
<?php
function findMajorityElement($arr)
{
sort($arr);
$n = count($arr);
$majorityCount = ceil($n / 2);
$current = null;
$count = 0;
foreach ($arr as $num) {
if ($num !== $current) {
$current = $num;
$count = 1;
} else {
$count++;
}
if ($count >= $majorityCount) {
return $num;
}
}
return null;
}
$arr1 = [3, 2, 3];
$result1 = findMajorityElement($arr1);
echo "Array: " . implode(", ", $arr1) . "\n";
echo "Majority Element: " .
($result1 !== null ? $result1 : "No Majority Element") . "\n";
$arr2 = [2, 2, 1, 1, 1, 2, 2];
$result2 = findMajorityElement($arr2);
echo "\nArray: " . implode(", ", $arr2) . "\n";
echo "Majority Element: " .
($result2 !== null ? $result2 : "No Majority Element") . "\n";
OutputArray: 3, 2, 3
Majority Element: 3
Array: 2, 2, 1, 1, 1, 2, 2
Majority Element: 2
Using Brute Force
In this method, we use the brute force approach that uses iteration through the array and count the occurrences of each element. The element that will have a count greater than n/2 is the majority element. We use the array_count_values() to count the occurrences of each element and null is displayed if none of them are in majority.
Example: To find the majority element of an array in PHP using brute force method.
PHP
<?php
function findMajorityElement($arr)
{
$n = count($arr);
$majority = null;
for ($i = 0; $i < $n; $i++) {
$count = 0;
for ($j = 0; $j < $n; $j++) {
if ($arr[$i] == $arr[$j]) {
$count++;
}
}
if ($count > ($n / 2)) {
$majority = $arr[$i];
break;
}
}
return $majority;
}
$arr = [3, 1, 3, 3, 2];
$majorityElement = findMajorityElement($arr);
if ($majorityElement !== null) {
echo "The majority element is: " . $majorityElement;
} else {
echo "No majority element exists in the array.";
}
OutputThe majority element is: 3
Using Boyer-Moore Majority Vote Algorithm
One used the Boyer-Moore Majority Vote Algorithm to find the majority element, assuming it exists (i.e., appears more than n/2 times in an array of size n). This algorithm has a time complexity of O(n), making it efficient as it finds the majority element in a single pass through the array.
Example: To find the majority element of an array in PHP using Boyer-Moore Majority Vote Algorithm.
PHP
<?php
function findMajorityElement($arr)
{
$n = count($arr);
$majority = null;
$candidate = null;
$count = 0;
for ($i = 0; $i < $n; $i++) {
if ($count == 0) {
$candidate = $arr[$i];
}
$count += ($arr[$i] == $candidate) ? 1 : -1;
}
$count = 0;
for ($i = 0; $i < $n; $i++) {
if ($arr[$i] == $candidate) {
$count++;
}
}
if ($count > ($n / 2)) {
$majority = $candidate;
}
return $majority;
}
$arr = [3, 1, 3, 3, 2];
$majorityElement = findMajorityElement($arr);
if ($majorityElement !== null) {
echo "The majority element is: " . $majorityElement;
} else {
echo "No majority element exists in the array.";
}
OutputThe majority element is: 3
Similar Reads
Find the Most Frequent Element in an Array in PHP Given an array, i.e. $arr, the task is to find the most frequent element in an array. There are multiple ways to solve this problem in PHP we will be going to discuss them. These problems can be used during data analysis, Web Analytics, or highly used in fraud detection. Example: Input: $array = [1,
2 min read
Find the Second Largest Element in an Array in PHP We will be given with an integer array, i.e. $array1 or $ array2, and the task is to find the second largest element in the array. There are multiple ways to approach and solve this problem however using sorting is the most common and concise approach as we use the rsort() function for further opera
3 min read
PHP Second most frequent element in an array Given an array we have to find the second most frequent element present in it. Examples: Input : array(3, 3, 4, 5, 5, 5, 9, 8, 8, 8, 8, 8); Output : Second most frequent element is: 5 Input : array("geeks", "for", "geeks"); Output : Second most frequent element is: for Here are some common approache
4 min read
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read
Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing
3 min read