Find the Most Frequent Element in an Array in PHP
Last Updated :
18 Jun, 2024
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, 3, 3, 3, 4];
Output: 3
These are the following ways to find the Most Frequent Element in an Array:
Using PHP array_count_values() method
The array_count_values() function allows to tally occurrences of each array element, returning an associative array with elements as keys and their frequencies as values. Using max() identifies the highest frequency, and array_search() retrieves the element associated with this peak frequency."
Example: To demonstrate finding the most frequent element in an arrya using arrya_count_values() method in PHP.
PHP
<?php
function findMostFrequent($arr)
{
$freqArray = array_count_values($arr);
$maxFreq = max($freqArray);
$mostFrequent = array_search($maxFreq, $freqArray);
return $mostFrequent;
}
$array = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4];
$mostFrequent = findMostFrequent($array);
echo "Most frequent element: " . $mostFrequent;
?>
OutputMost frequent element: 4
Using a forEach() loop and a frequency array
We'll initialize an empty associative array "count" to track element counts. As we iterate through each element in the input array, we ensure each element exists in "count" , initializing its count to 0 if necessary. We then increment the count for each element. If the current count surpasses the maximum count observed thus far, we update "maxCount" and "mostFrequent".
Example: To demonstrate finding the most frequent element in an arrya using forEach() loopin PHP.
PHP
<?php
function findMostFrequent($arr)
{
$freqArray = array();
foreach ($arr as $element) {
if (isset($freqArray[$element])) {
$freqArray[$element]++;
} else {
$freqArray[$element] = 1;
}
}
$maxFreq = max($freqArray);
$mostFrequent = array_search($maxFreq, $freqArray);
return $mostFrequent;
}
$array = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4];
$mostFrequent = findMostFrequent($array);
echo "Most frequent element: " . $mostFrequent;
?>
OutputMost frequent element: 4
Similar Reads
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
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
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
How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra
2 min read
PHP Program to Find the Maximum Element in a Matrix Given a matrix, the task is to find the maximum element of the matrix (arrays of arrays) in PHP. Finding the maximum element in a matrix is a common task that involves iterating through all the elements of the matrix and keeping track of the largest value found. Example: Input: matrix = [ [1, 2, 3],
4 min read