0
0
PhpProgramBeginner · 2 min read

PHP Program to Find Frequency of Elements

Use the PHP function array_count_values() to find the frequency of elements in an array, like $freq = array_count_values($array); which returns an associative array with elements as keys and their counts as values.
📋

Examples

Input[1, 2, 2, 3, 3, 3]
Output{"1":1,"2":2,"3":3}
Input["apple", "banana", "apple", "orange", "banana", "banana"]
Output{"apple":2,"banana":3,"orange":1}
Input[]
Output{}
🧠

How to Think About It

To find how many times each element appears, look at each item in the list and keep track of how many times you see it. You can use a container that stores each element as a key and increases its count every time you find it again.
📐

Algorithm

1
Get the input array of elements.
2
Create an empty map/dictionary to hold element counts.
3
For each element in the array, check if it is already in the map.
4
If yes, increase its count by one; if no, add it with count one.
5
Return the map/dictionary with elements and their frequencies.
💻

Code

php
<?php
$array = [1, 2, 2, 3, 3, 3];
$frequency = array_count_values($array);
print_r($frequency);
?>
Output
Array ( [1] => 1 [2] => 2 [3] => 3 )
🔍

Dry Run

Let's trace the array [1, 2, 2, 3, 3, 3] through the code.

1

Input array

The array is [1, 2, 2, 3, 3, 3].

2

Count frequencies

array_count_values counts each element's occurrences.

3

Result

The frequency array is [1 => 1, 2 => 2, 3 => 3].

ElementCount
11
22
33
💡

Why This Works

Step 1: Using array_count_values

The function array_count_values() takes an array and returns an associative array where keys are the unique elements and values are their counts.

Step 2: Counting elements

It loops internally through the array once, counting how many times each element appears.

Step 3: Output format

The output is easy to use because it directly maps each element to its frequency.

🔄

Alternative Approaches

Manual counting with foreach loop
php
<?php
$array = [1, 2, 2, 3, 3, 3];
$freq = [];
foreach ($array as $item) {
    if (isset($freq[$item])) {
        $freq[$item]++;
    } else {
        $freq[$item] = 1;
    }
}
print_r($freq);
?>
This method is more manual but helps understand how counting works internally.
Using array_reduce
php
<?php
$array = [1, 2, 2, 3, 3, 3];
$freq = array_reduce($array, function($carry, $item) {
    $carry[$item] = ($carry[$item] ?? 0) + 1;
    return $carry;
}, []);
print_r($freq);
?>
This uses a functional approach with <code>array_reduce</code> but is less straightforward for beginners.

Complexity: O(n) time, O(n) space

Time Complexity

The function loops through the array once, so the time grows linearly with the number of elements.

Space Complexity

It creates a new array to store counts, which can be as large as the number of unique elements.

Which Approach is Fastest?

array_count_values() is the fastest and simplest built-in method compared to manual loops or functional methods.

ApproachTimeSpaceBest For
array_count_values()O(n)O(n)Quick and simple frequency count
Manual foreach loopO(n)O(n)Understanding logic, custom processing
array_reduce()O(n)O(n)Functional programming style
💡
Use array_count_values() for a quick and efficient way to count element frequencies in PHP arrays.
⚠️
Beginners often try to count frequencies without checking if the element key exists, causing errors or wrong counts.