好的,所以我有一个不同的解决方案(一个不断破坏输入数组的解决方案),似乎比 OP 快 30%。但后来我把 Nigel Ren 的解决方案放在了替补席上,这个解决方案统治了所有这些。
编辑:除了任的解决方案有一个警告。它不适用于非标量,因为它将值用作数组键。虽然 OP 和我的解决方案确实如此(如果我们使用 strict=true 调整 array_keys 调用,令我惊讶的是,使用 strict=true 它会变得有点慢???)。
<?php
function solution(array $A) {
foreach ($A as $key => $value) {
$searchResults = array_keys($A, $value, true);
//print "Total number of $value found in Array is: " . count($searchResults) . "\n";
$checkNumber = count($searchResults);
if ($checkNumber == 1) {
//print "Unique value is: $value\n";
return $value;
}
//print "\n";
}
return null;
}
function solutionRen($a) {
$counts = @array_count_values($a); //disable warning when running over nonscalars
foreach ( $counts as $value => $count ) {
if ( $count == 1 ) {
return $value;
}
}
return false;
}
function solutionSlepic(array $A)
{
while (!empty($A)) {
$value = \array_shift($A);
$keys = \array_keys($A, $value, true);
if (empty($keys)) {
return $value;
}
foreach ($keys as $key) {
unset($A[$key]);
}
}
return null;
}
$range = \range(1,20000);
$input = \array_merge($range, $range, [1000001]);
$input = \array_merge([1000001], $range, $range);
$input = \array_merge($range, $range);
$input = \array_merge(\array_fill(0, 20000, 1), \array_fill(0, 20000, 2));
$input = [];
for($i=0; $i<20000; ++$i) {
$input[$i] = $input[20000 + $i] = new \stdClass();
}
$input[] = (object) ['unique' => true];
$start = \microtime(true);
$solutionOP = solution($input);
$timeOP = \microtime(true) - $start;
echo "OP: $solutionOP ({$timeOP})\n";
$start = \microtime(true);
$solutionRen = solutionRen($input);
$timeRen = \microtime(true) - $start;
echo "Ren: $solutionRen ({$timeRen})\n";
$start = \microtime(true);
$solutionSlepic = solutionSlepic($input);
$timeSlepic = \microtime(true) - $start;
echo "slepic: $solutionSlepic ({$timeSlepic})\n";
各种输入的输出:
// unnique valus is on end
OP: 1000001 (1.7094209194183)
Ren: 1000001 (0.00097393989562988)
slepic: 1000001 (1.1519079208374)
// unique value is the first
OP: 1000001 (0.00011515617370605)
Ren: 1000001 (0.0009620189666748)
slepic: 1000001 (0.00069785118103027)
// unique value not found among 20k distinct values, each twice in the set
OP: (1.728000164032)
Ren: (0.00064802169799805)
slepic: (1.18425989151)
// unque value not found among 2 distinct values, each 20k times in the set
OP: (6.4909980297089)
Ren: (0.00011396408081055)
slepic: (0.0016219615936279)
// 20000 distinct objects, each twice in the array and one unique on end
OP: (4.8111519813538)
stdClass Object
(
[unique] => 1
)
// Ren's solution is not capable of working with types other then those that can be keys of array, and so it didnt find the solution and instead yells 40k php warning which i have muted.
Ren: (0.013867139816284)
slepic: (2.5294151306152)
stdClass Object
(
[unique] => 1
)