Check and return duplicates array php

前端 未结 10 2114
我寻月下人不归
我寻月下人不归 2020-12-05 22:33

I would like to check if my array has any duplicates and return the duplicated values in an array. I want this to be as efficient as possible.

Example:



        
相关标签:
10条回答
  • 2020-12-05 23:30

    You can get the difference of the original array and a copy without duplicates using array_unique and array_diff_assoc:

    array_diff_assoc($arr, array_unique($arr))
    
    0 讨论(0)
  • 2020-12-05 23:31
    function returndup($array) 
    {
        $results = array();
        $duplicates = array();
        foreach ($array as $item) {
            if (in_array($item, $results)) {
                $duplicates[] = $item;
            }
    
            $results[] = $item;
        }
    
        return $duplicates;
    }
    
    0 讨论(0)
  • 2020-12-05 23:34

    in addition to gumbo's answer:

    function returndup($arr)
    {
      return array_diff_key($arr, array_unique($arr));
    }
    
    0 讨论(0)
  • 2020-12-05 23:35

    I did some tests and indeed @user187291's variant is the fastest. But, it turns out that @Gumbo's and @faebser's alternative are almost as fast, @faebser's being just slightly faster than @Gumbo's and sometimes even fastest of all.

    Here's the code I used

    $array = array(1, "hello", 1, "world", "hello");
    $times = 1000000;
    
    $start = microtime(true);
    for ($i = 0; $i < $times; $i++) {
        $dups = array();
        foreach(array_count_values($array) as $val => $c)
            if( $c > 1) $dups[] = $val;
    }
    $end = microtime(true);
    
    echo 'variant 1 (user187291): ' . ($end - $start);
    echo '<br><br><br>';
    
    $start = microtime(true);
    for ($i = 0; $i < $times; $i++)
        $dups = array_unique(array_diff_assoc($array, array_unique($array)));
    $end = microtime(true);
    
    echo 'variant 2 (JAL): ' . ($end - $start);
    echo '<br><br><br>';
    
    $start = microtime(true);
    for ($i = 0; $i < $times; $i++)
        $dups = array_diff_assoc($array, array_unique($array));
    $end = microtime(true);
    
    echo 'variant 3 (Gumbo): ' . ($end - $start);
    echo '<br><br><br>';
    
    $start = microtime(true);
    for ($i = 0; $i < $times; $i++)
        $dups = array_diff_key($array, array_unique($array));
    $end = microtime(true);
    
    echo 'variant 4 (faebser): ' . ($end - $start);
    echo '<br><br><br>';
    
    0 讨论(0)
提交回复
热议问题