Is there a two-dimensional array merge deduplication algorithm whose complexity is O (n )?
Is there a two-dimensional array merge deduplication algorithm whose complexity is O (n )?
Reply content:
Is there a two-dimensional array merge deduplication algorithm whose complexity is 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 posted my thoughts to do this. Ifn
The sum of the number of elements in all arrays.
This must use a 2 loop. The definition of n should be the sum of the elements of multiple associated arrays. a double loop is even O (n ).
After serialize is used as a hash comparison, what about this idea? I will not write code on the phone...