There is no two-dimensional array combining the de-weight algorithm with the complexity O (n)
Reply content:
There is no two-dimensional array combining the de-weight algorithm with the complexity O (n)
$a = [ ['id'=>1], ['id'=>2], ['id'=>3], ['id'=>4], ['id'=>5], ['id'=>6]];$b = [ ['id'=>5], ['id'=>6], ['id'=>7],];$c = [ ['id'=>8], ['id'=>9]];function array_unique_merge() { $params = func_get_args(); $result = []; $hashmap = []; $arr_count = count($params); for($i = 0; $i<$arr_count; $i++) { foreach($params[$i] as $key => $val) { $md5 = md5(json_encode($val)); if (!isset($hashmap[$md5])) { $hashmap[$md5] = true; $result[] = $val; } } } return $result;}print_r(array_unique_merge($a, $b, $c));
I just post it and I think I can do it. If n
it refers to the sum of the number of elements in all arrays.
This has to be in 2 loops, and the definition of n should be the sum of the elements of multiple associative arrays, even O (n) for a double loop.
Serialize later as a hash comparison, this idea? I will not write the code ...