Check and return duplicates array php

Viewed 79109

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:

$array = array( 1, 2, 2, 4, 5 );
function return_dup($array); // should return 2

$array2 = array( 1, 2, 1, 2, 5 );
function return_dup($array2); // should return an array with 1,2

Also the initial array is always 5 positions long

10 Answers

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>';

I have found another way to return duplicates in an array

function printRepeating($arr, $size) 
{ 
    $i; 
    $j; 
    for($i = 0; $i < $size; $i++) 
        for($j = $i + 1; $j < $size; $j++) 
            if($arr[$i] == $arr[$j]) 
                echo $arr[$i], " "; 
}  



printRepeating($array, sizeof($array,0);

If you need a solution that will work with an array of arrays (or any array values other than integers or strings) try this:

function return_dup( $arr ) {
    $dups = array();
    $temp = $arr;
    foreach ( $arr as $key => $item ) {
        unset( $temp[$key] );
        if ( in_array( $item, $temp ) ) {
            $dups[] = $item;
        }
    }
    return $dups;
}


$arr = array(
    array(
        0 => 'A',
        1 => 'B',
    ),
    array(
        0 => 'A',
        1 => 'B',
    ),
    array(
        0 => 'C',
        1 => 'D',
    ),
    array(
        0 => 'C',
        1 => 'D',
    ),
    array(
        0 => 'E',
        1 => 'F',
    ),
    array(
        0 => 'F',
        1 => 'E',
    ),
    array(
        0 => 'Y',
        1 => 'Z',
    ),
);

var_export( return_dup( $arr ) );
/*
array (
    0 => array (
        0 => 'A',
        1 => 'B',
    ),
    1 => array (
        0 => 'C',
        1 => 'D',
    ),
)
*/

$duplicate_array = array();

  for($i=0;$i<count($array);$i++){

    for($j=0;$j<count($array);$j++){

      if($i != $j && $array[$i] == $array[$j]){

        if(!in_array($array[$j], $duplicate_array)){

          $duplicate_array[] = $array[$j];

        }

      }

    }    

  }
Related