Comprehensive Guide to Detecting and Counting Duplicate Values in PHP Arrays

Nov 24, 2025 · Programming · 9 views · 7.8

Keywords: PHP | array_processing | duplicate_counting

Abstract: This article provides an in-depth exploration of methods for detecting and counting duplicate values in PHP arrays. It focuses on the array_count_values() function for efficient value frequency counting, compares it with array_unique() based approaches for duplicate detection, and demonstrates formatted output generation. The discussion extends to cross-language techniques inspired by Excel's duplicate handling methods, offering comprehensive technical insights.

Problem Context and Requirements

When working with one-dimensional array data, there is often a need to detect duplicate values and perform statistical analysis. For example, given an array containing fruit names:

$array = [
    'apple',
    'orange',
    'pear',
    'banana',
    'apple',
    'pear',
    'kiwi',
    'kiwi',
    'kiwi'
];

The desired output format is:

apple (2)
orange
pear (2)
banana
kiwi (3)

This requirement is common in data processing, statistical analysis, and various other scenarios.

Core Solution: The array_count_values() Function

PHP provides the built-in array_count_values() function specifically designed for this type of problem. This function takes an array as a parameter and returns a new array where keys are the original array values and values are the count of occurrences.

Basic Usage Example

$array = array('apple', 'orange', 'pear', 'banana', 'apple',
'pear', 'kiwi', 'kiwi', 'kiwi');

$counts = array_count_values($array);
print_r($counts);

Execution result:

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

Formatted Output Implementation

To achieve the desired output format, iterate through the result array:

$counts = array_count_values($array);

foreach ($counts as $value => $count) {
    if ($count > 1) {
        echo $value . ' (' . $count . ')' . "\n";
    } else {
        echo $value . "\n";
    }
}

This code will output:

apple (2)
orange
pear (2)
banana
kiwi (3)

Supplementary Method: Duplicate Detection

In some scenarios, you may only need to detect whether duplicate values exist in the array without detailed statistics. This can be achieved using the array_unique() function combined with count():

if (count(array_unique($array)) < count($array)) {
    // Array has duplicates
    echo 'Array contains duplicate values';
} else {
    // Array does not have duplicates
    echo 'Array contains no duplicate values';
}

This method determines the presence of duplicates by comparing the length of the deduplicated array with the original array length.

Cross-Language Technical References

Similar duplicate value processing requirements exist in other programming languages and tools. For example, in Excel, unique value counting can be achieved through various methods:

Advanced Filter Method

Using Excel's Advanced Filter feature to extract unique values: Select the data range, choose "Advanced" from the Data tab, check the "Unique records only" option, copy results to a new location, then use the ROWS function to count the quantity.

Formula Combination Method

Using combinations of IF, SUM, FREQUENCY, MATCH, and LEN functions to calculate unique value counts. This method requires entering array formulas, returning occurrence counts for the first instance of specific values and zero for subsequent occurrences.

Performance Analysis and Best Practices

The array_count_values() function has a time complexity of O(n), where n is the array length, making it the optimal choice for this type of problem. In comparison, methods that first use array_unique() then perform counting have time complexity of O(n log n) or higher, making them less efficient.

In practical applications, it is recommended to:

Extended Application Scenarios

This duplicate value counting technique can be applied to:

By flexibly applying these methods, various data statistics and analysis requirements can be efficiently addressed.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.