There is no two-dimensional array combining the de-weight algorithm with the complexity O (n)

Source: Internet
Author: User

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.