Find Duplicate

Find the first duplicate element that appears in an array using a hash set for efficient lookup.

Code

Algorithms
$seen = [];
$dup = null;
foreach ($arr as $x) {
    if (isset($seen[$x])) { $dup = $x; break; }
    $seen[$x] = true;
}
return $dup;

Parameters

Array to check

Server

More PHP Snippets