$v2) { $j++; } else { if (!self::contains($set, $v1)) { array_push($set, $v1); } $i++; $j++; } } foreach ($p as $v) { if (self::contains($set, $v) && !self::contains($ans, $v)) { array_push($ans, $v); } } return $ans; } public static function intersection3(array $p, array $q): array { if (empty($p) || empty($q)) { return []; } $helper = static function (array $nums, int $target): bool { [$low, $high] = [0, count($nums) - 1]; while ($low <= $high) { $mid = $low + intdiv($high - $low, 2); if ($nums[$mid] > $target) { $high = $mid - 1; } elseif ($nums[$mid] < $target) { $low = $mid + 1; } else { return true; } } return false; }; sort($p); $ans = []; foreach ($q as $v) { if ($helper($p, $v) && !self::contains($ans, $v)) { array_push($ans, $v); } } return $ans; } private static function contains(array $array, int $value): bool { return in_array($value, $array, true); } }