查找PHP数组中最频繁的数字
我在PHP中有一个带有重复数字的数组,我希望找到最频繁的,但只有在只有一个重复数字的情况下查找PHP数组中最频繁的数字,php,arrays,Php,Arrays,我在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($
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\u reduce
以找到最大值,但由于Array\u reduce
不允许您访问iterable的键,因此您必须执行额外的转换
相反,我建议您通过扩展
完整脚本
$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())
);
$numbers=[0,1,1,1,2,3,4,4,5,6,7,7,7,8,9];
类MaxHeap扩展了SplMaxHeap{
公共职能比较($a$b){
if(当前($a)<当前($b))
返回-1;
elseif(当前($a)>当前($b))
返回1;
其他的
返回0;
}
}
$heap=新的MaxHeap();
foreach(数组\计数\值($numbers)为$n=>$count)
$heap->insert([$n=>$count]);
printf(“%d是最常见的数字,出现了%d次”,
键($heap->top()),
当前($heap->top())
);
修订历史记录 我不知道PHP的本机函数。我删除了更复杂的
array\u reduce
,以支持这个超级专业化的函数。谢谢,@CBroe
我在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);
}
}
你的方法似乎相当复杂
我是这样做的:
$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.';
}
我不知道是否有多个最频繁的元素。例如,如果我在数组中再添加一个1,我会发现1是最频繁的。我想知道是否只有一个是最常见的,并且只有一个是准确的数字。那么,这个数字的“问题”到底是什么?如果是这种情况,那么是的,它将返回堆中第一个最频繁的元素。什么是没有问题的答案?它是否应该同时返回两者?如果出现平局,它是否应该返回较大的数字?如果出现平局,是否因为没有最常见的号码而返回
null
If?我的观点是,这不是问题,行为只是没有定义。定义您希望发生的事情,我们可以让它发生。lol我不知道PHP有一个数组\u count\u value
函数。。。哦,惊喜不断。我在回答中删除了数组\u reduce
,以支持此内置函数。这是否回答了您的问题?
$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.';
}