You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP实现数组奇偶统计:相同数字每出现两次仅计1次

Solution for Counting Odd/Even Numbers with Pair-Based Rules

Let's fix this problem step by step. Your current code only checks if each element is odd or even, but we need to track occurrences per number and apply the rule: every two instances of the same number count as one entry (we'll cover both full-pair and partial-pair interpretations below).

Full Working Code

<?php
$myarray = array(5,5,0,1,2,1,1,6,1);

// Step 1: Count how many times each number appears
$numberOccurrences = array_count_values($myarray);

// Initialize storage for results and totals
$oddNumberStats = [];
$evenNumberStats = [];
$totalOddCount = 0;
$totalEvenCount = 0;

// Step 2: Apply the counting rule and categorize numbers
foreach ($numberOccurrences as $number => $count) {
    // Choose one of the two calculation methods below:
    
    // Option 1: Partial pairs count as 1 (single occurrence = 1 entry, 2 occurrences =1, 3=2, etc.)
    $calculatedCount = (int)ceil($count / 2);
    
    // Option 2: Only full pairs count (single occurrence =0, 2=1, 3=1, 4=2, etc.)
    // $calculatedCount = intdiv($count, 2);

    // Categorize as odd or even
    if ($number % 2 === 0) {
        $evenNumberStats[$number] = $calculatedCount;
        $totalEvenCount += $calculatedCount;
    } else {
        $oddNumberStats[$number] = $calculatedCount;
        $totalOddCount += $calculatedCount;
    }
}

// Step 3: Output detailed results
echo "Detailed Number Stats:\n";
foreach ($oddNumberStats as $num => $stat) {
    echo "$num:$stat ($num is odd, appeared {$numberOccurrences[$num]} times, counted as $stat entries)\n";
}
foreach ($evenNumberStats as $num => $stat) {
    echo "$num:$stat ($num is even, appeared {$numberOccurrences[$num]} times, counted as $stat entries)\n";
}

// Output total counts for quick reference
echo "\nTotal Odd Entries: $totalOddCount\n";
echo "Total Even Entries: $totalEvenCount\n";
?>

How This Works

Step 1: Count Occurrences

We use PHP's built-in array_count_values() function to efficiently tally how many times each number shows up in the input array. This is cleaner and faster than writing a custom counting loop.

Step 2: Apply the Rule

There are two common interpretations of your rule, and you can switch between them by uncommenting the relevant line:

  • Partial pairs count: If a number appears once, it counts as 1 entry; every additional pair adds another entry. Use ceil($count / 2) for this.
  • Full pairs only: Only complete pairs count—single occurrences don't contribute. Use intdiv($count, 2) for this.

Your example (2 instances of 5 =1 entry, 4 instances of 1=2 entries) works with both options, but the first one is more intuitive for handling single occurrences (like 0, 2, 6 in your input).

Step 3: Categorize and Output

We separate results into odd and even numbers, then print both detailed per-number stats and total counts for each category.

Example Output

When running the code with your input array, you'll get:

Detailed Number Stats:
5:1 (5 is odd, appeared 2 times, counted as 1 entries)
1:2 (1 is odd, appeared 4 times, counted as 2 entries)
0:1 (0 is even, appeared 1 times, counted as 1 entries)
2:1 (2 is even, appeared 1 times, counted as 1 entries)
6:1 (6 is even, appeared 1 times, counted as 1 entries)

Total Odd Entries: 3
Total Even Entries: 3

This includes the exact results you mentioned for the odd numbers (5:1, 1:2), plus the even number counts your original code didn't handle.

内容的提问来源于stack exchange,提问作者Tareq Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:22:57