Open In App

Find the Majority Element of an Array in PHP

Last Updated : 19 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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";

Output
Array: 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.";
}

Output
The 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.";
}

Output
The majority element is: 3

Next Article

Similar Reads