Current Location: Home> Latest Articles> array_count_values() + array_keys() Get high-frequency elements

array_count_values() + array_keys() Get high-frequency elements

M66 2025-06-07

How to get high-frequency elements in an array through array_count_values() and array_keys() functions?

In PHP, when processing arrays, we often need to find out the high-frequency elements in the array (that is, the elements that appear the most). PHP provides some built-in functions to help us implement this function, where array_count_values() and array_keys() are two very useful functions.

This article will introduce how to obtain high-frequency elements in an array through these two functions and demonstrate it through sample code.

1. array_count_values() function

The array_count_values() function can return an associative array, the key is the elements in the input array, and the value is the number of times these elements appear in the original array.

Example:

 <?php
// Example array
$array = array("apple", "banana", "apple", "orange", "banana", "apple");

// use array_count_values Get the number of occurrences of an element
$counts = array_count_values($array);

// Number of times the output element appears
print_r($counts);
?>

Output result:

 Array
(
    [apple] => 3
    [banana] => 2
    [orange] => 1
)

In this example, we can see that the array_count_values() function successfully counts the number of occurrences of each element in the array.

2. Find high-frequency elements

Next, we want to find the element that appears the most. To do this, we can combine array_count_values() and array_keys() to achieve it.

Example:

 <?php
// Example array
$array = array("apple", "banana", "apple", "orange", "banana", "apple");

// Get the number of occurrences of an element
$counts = array_count_values($array);

// Find the maximum value
$maxCount = max($counts);

// Get elements whose occurrences are equal to the maximum
$mostFrequent = array_keys($counts, $maxCount);

// Output high frequency elements
print_r($mostFrequent);
?>

Output result:

 Array
(
    [0] => apple
)

In this example, we first count the number of occurrences of each element in the array through array_count_values() , then use max() to get the maximum occurrences, and then use array_keys() to get all elements with the maximum occurrences. In this example, "apple" appears the most times, so it is a high frequency element.

3. Handle multiple situations

If multiple elements in the array appear the same number of times and are all the highest frequency elements, array_keys() returns an array containing all of these elements.

Example:

 <?php
// Example array,Multiple high frequency elements
$array = array("apple", "banana", "apple", "orange", "banana", "apple", "orange");

// Get the number of occurrences of an element
$counts = array_count_values($array);

// Find the maximum value
$maxCount = max($counts);

// Get elements whose occurrences are equal to the maximum
$mostFrequent = array_keys($counts, $maxCount);

// Output high frequency elements
print_r($mostFrequent);
?>

Output result:

 Array
(
    [0] => apple
    [1] => banana
)

In this example, both "apple" and "banana" are the most common elements, so they are both considered high-frequency elements.

in conclusion

By using the array_count_values() and array_keys() functions, we can easily find high-frequency elements in the array. First, we use array_count_values() to count the number of occurrences of elements, and then use max() and array_keys() functions to find and return the element with the most occurrences. For cases where there are multiple high-frequency elements, array_keys() returns all of these elements.