Check and return duplicates array php

PhpArraysDuplicates

Php Problem Overview


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

Php Solutions


Solution 1 - Php

this will be ~100 times faster than array_diff

$dups = array();
foreach(array_count_values($arr) as $val => $c)
	if($c > 1) $dups[] = $val;

Solution 2 - Php

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))

Solution 3 - Php

function array_dup($ar){
   return array_unique(array_diff_assoc($ar,array_unique($ar)));
}

Should do the trick.

Solution 4 - Php

You can do like this:

function showDups($array)
{
  $array_temp = array();

   foreach($array as $val)
   {
     if (!in_array($val, $array_temp))
     {
       $array_temp[] = $val;
     }
     else
     {
       echo 'duplicate = ' . $val . '<br />';
     }
   }
}


$array = array(1,2,2,4,5);
showDups($array);

Output:

duplicate = 2

Solution 5 - Php

function returndup($array) 
{
    $results = array();
    $duplicates = array();
    foreach ($array as $item) {
        if (in_array($item, $results)) {
            $duplicates[] = $item;
        }

        $results[] = $item;
    }

    return $duplicates;
}

Solution 6 - Php

in addition to gumbo's answer:

function returndup($arr)
{
  return array_diff_key($arr, array_unique($arr));
}

Solution 7 - Php

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

Solution 8 - Php

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

Solution 9 - Php

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',
    ),
)
*/

Solution 10 - Php

$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];

        }

      }

    }    

  }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNVGView Question on Stackoverflow
Solution 1 - Phpuser187291View Answer on Stackoverflow
Solution 2 - PhpGumboView Answer on Stackoverflow
Solution 3 - PhpJALView Answer on Stackoverflow
Solution 4 - PhpAsif RazaView Answer on Stackoverflow
Solution 5 - PhpRonn0View Answer on Stackoverflow
Solution 6 - PhpfaebserView Answer on Stackoverflow
Solution 7 - PhpStefan GabosView Answer on Stackoverflow
Solution 8 - PhpNemoView Answer on Stackoverflow
Solution 9 - PhpsquarecandyView Answer on Stackoverflow
Solution 10 - Phpraviranjan prasadView Answer on Stackoverflow