How can PHP developers efficiently check for the presence of specific patterns, such as pairs or triplets, in an array of numbers?

To efficiently check for the presence of specific patterns, such as pairs or triplets, in an array of numbers, PHP developers can use a loop to iterate through the array and keep track of the frequency of each number using an associative array. By comparing the frequencies of the numbers, developers can easily identify the desired patterns.

function checkPatterns($numbers, $patternLength) {
    $frequency = array();
    
    foreach($numbers as $num) {
        if(isset($frequency[$num])) {
            $frequency[$num]++;
        } else {
            $frequency[$num] = 1;
        }
    }
    
    foreach($frequency as $num => $count) {
        if($count >= $patternLength) {
            echo "Pattern found: $num appears $count times\n";
        }
    }
}

$numbers = [1, 2, 2, 3, 4, 4, 4, 5];
$patternLength = 3;

checkPatterns($numbers, $patternLength);