在PHP数组中找到最频繁的数字

3

我在PHP中有一个包含重复数字的数组,我想找到最频繁出现的数字,但只限于当该数字仅出现一次时。

while (count(array_count_values($arr)) > 1) {
$minim = min(array_count_values($arr));
while ($minim == min(array_count_values($arr))) {
    unset($arr[array_search(array_search(min(array_count_values($arr)), array_count_values($idList)), $arr)]);
    $arr = array_splice($arr, 0, 1);
}
}

在我的代码中,第一个while循环一直运行,直到数组中只剩下一个数字(多次出现),而第二个while循环则是删除出现频率较低的数字。 我的问题是,当调用第二个min()函数时,我收到了以下错误消息:"Array must contain at least one element"。


这个回答解决了你的问题吗?获取数组中最常重复的值 - gen
此问题缺少 [mcve]。 - mickmackusa
2个回答

2

我在PHP中有一个重复数字的数组,我想找到最频繁出现的,但仅当只有一个时。

您的方法似乎相当复杂。

这是我的做法:

$numbers = [1, 6, 5, 6, 2, 1, 6, 7, 8]; // positive test case
//$numbers = [1, 6, 5, 6, 2, 1, 6, 7, 8, 1];  // negative test case

$count = array_count_values($numbers); // get count of occurrence for each number

arsort($count); // sort by occurrence, descending

$first = key($count); // get key of first element, because that is the/one
                      // of the highest number(s)
$count_first = current($count); // get occurrence for first array value
$count_second = next($count); // get occurrence for second array value

if($count_first != $count_second) { // did they occur in different frequencies?
  echo $first . ' occurred most in input array.';
}
else {
  echo 'input array contained multiple values with highest occurrence.';
}

哈哈,我从没想到 PHP 有一个 array_count_values 函数... 哦 PHP,你总是能带来惊喜。我用这个内置函数替换了我的答案中的 array_reduce - Mulan

1
你可以对计数数组执行array_reduce操作以找到最大值,但是由于array_reduce不允许访问可迭代对象的键,所以你需要进行额外的转换。

相反地,我建议你通过扩展SplMaxHeap来构建自己的MaxHeap

class MaxHeap extends SplMaxHeap {
  public function compare($a, $b) {
    if (current($a) < current($b))
      return -1;
    elseif (current($a) > current($b))
      return 1;
    else
      return 0;
  }
}

然后我们可以将其用作这样 - 答案显示[ 7 => 4 ],这意味着:7是最常见的数字,出现4次。
$heap = new MaxHeap();
foreach (array_count_values($numbers) as $n => $count)
  $heap->insert([$n => $count]);

print_r($heap->top());
// [ 7 => 4 ]

printf("%d is the most common number, appearing %d times",
  key($heap->top()),
  current($heap->top())
);
// 7 is the most common number, appearing 4 times

完整脚本
$numbers = [0, 1, 1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 7, 7, 8, 8, 9];

class MaxHeap extends SplMaxHeap {
  public function compare($a, $b) {
    if (current($a) < current($b))
      return -1;
    elseif (current($a) > current($b))
      return 1;
    else
      return 0;
  }
}

$heap = new MaxHeap();
foreach (array_count_values($numbers) as $n => $count)
  $heap->insert([$n => $count]);

printf("%d is the most common number, appearing %d times",
  key($heap->top()),
  current($heap->top())
);

修订历史

我之前不知道 PHP 自带的 array_count_values 函数。为了使用这个非常专业的函数,我把原先较为复杂的 array_reduce 函数删除了。感谢 @CBroe。


我不确定是否存在多个最频繁的元素。例如,如果我向数组中再添加一个1,则会得到1是最频繁的结果。我想知道是否只有一个最频繁的元素,只有在这种情况下才能得到确切的数字。 - utinum
@utinum 那么这个“问题”到底是什么?如果是这样,那么它将返回堆中最常见的第一个元素。什么才是非问题的答案呢?它应该同时返回两个吗?如果有平局,它应该返回更大的数字吗?如果有平局,因为没有常见的数字,它应该返回null吗?我的观点是,这不是一个“问题”,行为只是未定义的。定义你想要发生什么,我们可以让它发生。 - Mulan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接