<?php
function findFirstDuplicate($arr) {
$seen = [];
foreach ($arr as $num) {
if (isset($seen[$num])) {
return $num;
}
$seen[$num] = true;
}
return null;
}
$fn = fn($arr) => findFirstDuplicate($arr);
echo $fn([2, 5, 1, 2, 3, 5]); // Output: 2
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter