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 returndup($array) should return 2 ;
if array is array(1,2,1,2,5);
it should return an array with 1,2
Also the initial array is always 5 positions long
in addition to gumbo's answer:
$duplicate_array = array();
Should do the trick.
You can get the difference of the original array and a copy without duplicates using
array_unique
andarray_diff_assoc
:I have found another way to return duplicates in an array