php:检查数组是否有重复项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3145607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php: check if an array has duplicates
提问by Mala
I'm sure this is an extremely obvious question, and that there's a function that does exactly this, but I can't seem to find it. In PHP, I'd like to know if my array has duplicates in it, as efficiently as possible. I don't want to remove them like array_uniquedoes, and I don't particularly want to run array_uniqueand compare it to the original array to see if they're the same, as this seems very inefficient. As far as performance is concerned, the "expected condition" is that the array has no duplicates.
我确信这是一个非常明显的问题,并且有一个函数可以做到这一点,但我似乎找不到它。在 PHP 中,我想尽可能有效地知道我的数组中是否有重复项。我不想像那样删除它们array_unique,我也不想运行array_unique并将其与原始数组进行比较以查看它们是否相同,因为这似乎非常低效。就性能而言,“预期条件”是数组没有重复项。
I'd just like to be able to do something like
我只想能够做类似的事情
if (no_dupes($array))
// this deals with arrays without duplicates
else
// this deals with arrays with duplicates
Is there any obvious function I'm not thinking of?
How to detect duplicate values in PHP array?
has the right title, and is a very similar question, however if you actually read the question, he's looking for array_count_values.
有没有我没有想到的明显功能?
如何检测PHP数组中的重复值?
有正确的标题,是一个非常相似的问题,但是如果你真的阅读了这个问题,他正在寻找 array_count_values。
采纳答案by Mike Sherov
You can do:
你可以做:
function has_dupes($array) {
$dupe_array = array();
foreach ($array as $val) {
if (++$dupe_array[$val] > 1) {
return true;
}
}
return false;
}
回答by Jason McCreary
I know you are not after array_unique(). However, you will not find a magicalobviousfunction nor will writing one be faster than making use of the native functions.
我知道你不是在追求array_unique()。但是,您不会找到一个神奇的明显函数,编写一个函数也不会比使用本机函数更快。
I propose:
我提议:
function array_has_dupes($array) {
// streamline per @Felix
return count($array) !== count(array_unique($array));
}
Adjust the second parameter of array_unique()to meet your comparison needs.
调整第二个参数array_unique()以满足您的比较需要。
回答by s3m3n
? PERFORMANCE SOLUTION ?
? 性能解决方案?
If you care about performance and micro-optimizations check this one-liner:
如果您关心性能和微优化,请检查此单行:
function no_dupes(array $input_array) {
return count($input_array) === count(array_flip($input_array));
}
Description:
描述:
Function compares number of array elements in $input_arraywith array_flip'ed elements. Values become keys and guess what - keys must be unique in associative arrays so not unique values are lost and final number of elements is lower than original.
函数将数组元素的数量$input_array与array_flip的元素进行比较。值成为键并猜测是什么 - 键在关联数组中必须是唯一的,因此不会丢失唯一值并且最终元素数低于原始值。
As said in manualarray keys can be only type of intor stringso this is what you can have in original array values to compare, otherwise PHP will start castingwith unexpected results.
正如所说的手动阵列键可以只键入int或string所以这是你可以在原来的数组值进行比较,否则PHP将开始铸造有意想不到的效果。
PROOF FOR 10M RECORDS ARRAY
1000 万条记录阵列的证明
- Most voted solution: 14.187316179276s
- Accepted solution: 2.0736091136932s
- This answer solution: 0.14155888557434s /10
- 投票最多的解决方案:14.187316179276s
- 接受的解决方案:2.0736091136932s
- 这个答案解决方案:0.14155888557434s /10
Test case:
测试用例:
<?php
$elements = array_merge(range(1,10000000),[1]);
$time = microtime(true);
accepted_solution($elements);
echo 'Accepted solution: ', (microtime(true) - $time), 's', PHP_EOL;
$time = microtime(true);
most_voted_solution($elements);
echo 'Most voted solution: ', (microtime(true) - $time), 's', PHP_EOL;
$time = microtime(true);
this_answer_solution($elements);
echo 'This answer solution: ', (microtime(true) - $time), 's', PHP_EOL;
function accepted_solution($array){
$dupe_array = array();
foreach($array as $val){
// sorry, but I had to add below line to remove millions of notices
if(!isset($dupe_array[$val])){$dupe_array[$val]=0;}
if(++$dupe_array[$val] > 1){
return true;
}
}
return false;
}
function most_voted_solution($array) {
return count($array) !== count(array_unique($array));
}
function this_answer_solution(array $input_array) {
return count($input_array) === count(array_flip($input_array));
}
Notice that accepted solution might be faster in certain condition when not unique values are near the beginning of huge array.
请注意,当不是唯一值靠近大数组的开头时,在某些情况下接受的解决方案可能会更快。
回答by Andrew
$hasDuplicates = count($array) > count(array_unique($array));
Will be trueif duplicates, or falseif no duplicates.
将是true如果重复,或者false如果没有重复。
回答by SpreadYourWings
$duplicate = false;
if(count(array) != count(array_unique(array))){
$duplicate = true;
}
回答by micadelli
Here's my take on this… after some benchmarking, I found this to be the fastest method for this.
这是我对此的看法……经过一些基准测试后,我发现这是最快的方法。
function has_duplicates( $array ) {
return count( array_keys( array_flip( $array ) ) ) !== count( $array );
}
…or depending on circumstances this could be marginally faster.
……或者根据情况,这可能会稍微快一点。
function has_duplicates( $array ) {
$array = array_count_values( $array );
rsort( $array );
return $array[0] > 1;
}
回答by Miles Bennet
Keep it simple, silly! ;)
保持简单,愚蠢!;)
Simple OR logic...
简单的 OR 逻辑...
function checkDuplicatesInArray($array){
$duplicates=FALSE;
foreach($array as $k=>$i){
if(!isset($value_{$i})){
$value_{$i}=TRUE;
}
else{
$duplicates|=TRUE;
}
}
return ($duplicates);
}
Regards!
问候!
回答by Muhammad Raheel
Find this useful solution
找到这个有用的解决方案
function get_duplicates( $array ) {
return array_unique( array_diff_assoc( $array, array_unique( $array ) ) );
}
After that count result if greater than 0 than duplicates else unique.
在该计数结果之后,如果大于 0 则重复其他唯一。
回答by Bwmat
Two ways to do it efficiently that I can think of:
我能想到的两种有效地做到这一点的方法:
inserting all the values into some sort of hashtable and checking whether the value you're inserting is already in it(expected O(n) time and O(n) space)
sorting the array and then checking whether adjacent cells are equal( O(nlogn) time and O(1) or O(n) space depending on the sorting algorithm)
将所有值插入某种哈希表并检查您插入的值是否已经在其中(预期 O(n) 时间和 O(n) 空间)
对数组进行排序,然后检查相邻单元格是否相等(O(nlogn) 时间和 O(1) 或 O(n) 空间取决于排序算法)
stormdrain's solution would probably be O(n^2), as would any solution which involves scanning the array for each element searching for a duplicate
Stormdrain 的解决方案可能是 O(n^2),任何涉及扫描数组的每个元素搜索重复项的解决方案也是如此
回答by Abraham Romero
I'm using this:
我正在使用这个:
if(count($array)==count(array_count_values($array))){
echo("all values are unique");
}else{
echo("there's dupe values");
}
I don't know if it's the fastest but works pretty good so far
我不知道它是否是最快的,但到目前为止效果很好

