Let's say I have an array of n elements(numbers or words), I want to find all the elements which occur more than once in the array. What is the most efficient approach of doing this in terms of performance?
PS: I can sort the array first, but only if that doesn't affect the total performance much. Also, though this is mainly php, i would like to know a algorithm or logic that can be implemented on other platforms too.
You can use array_count_values and array_filter
$array = array(1, "hello", 1, "world", "hello");
$new=array_filter(array_count_values($array),'custom_filter');
print_r($new);
function custom_filter($val)
{
return $val > 1;
}
output
Array
(
[1] => 2
[hello] => 2
)
$lookup = array();
foreach($array as $v) {
if (!isset($lookup[$v]))
$lookup[$v] = false;
else if ($lookup[$v] == false) {
echo "Duplicate $v
";
$lookup[$v] = true;
}
}
Hope this will work
array_unique(array_diff($inputArray, array_unique($inputArray)));
There is a array_count_values() function provided by PHP itself.
It does more than what you need, but should be quite fast since it's compiled-in..
Then, of course, you need to filter out the result for keys which value is > 1.
EDIT
If you want a one-liner:
$a = array('a','b','c','a','a','b','d','e');
array_keys(array_filter(array_count_values($a), create_function('$x', 'return $x>1;')));
// array (0 => 'a', 1 => 'b');