Find the Non-Repeating Elements in an Array

后端 未结 4 1873
别跟我提以往
别跟我提以往 2021-01-23 04:24

My array is :

$array= array(4,3,4,3,1,2,1);

And I\'d like to output it like below:

Output = 2 

(As 2 i

4条回答
  •  半阙折子戏
    2021-01-23 04:51

    One-liner with no loops: (Demo)

    var_export(array_keys(array_intersect(array_count_values($array),[1])));
    

    The breakdown:

    array_keys(                          // return the remaining keys from array_count_values 
        array_intersect(                 // filter the first array by second
            array_count_values($array),  // count number of occurrences of each value
            [1]                          // identify the number of occurrences to keep
        )
    )
    

    if you (or any future reader) wants to keep more values, replace the second parameter/array in array_intersect(). for instance: you want to keep 1,2,and 3: array(1,2,3) or [1,2,3]

    p.s. For the record, you can use array_filter() with a custom function to omit all non-1 count values, but I have used array_intersect() because the syntax is more brief and IMO easier to read.


    p.s. thought I'd revisit and include a PHP7.4 technique and compare against other function-based techniques...

    Code: (Demo)

    $numbers = [4, 3, 4, 3, 1, 2, 1];
    var_export(
        array_keys(
            array_intersect(
                array_count_values($numbers),
                [1]
            )
        )
    );
    
    echo "\n---\n";
    var_export(
        array_keys(
            array_filter(
                array_count_values($numbers),
                function($count) {
                    return $count === 1;
                }
            )
        )
    );
    
    echo "\n---\n";
    // PHP7.4+
    var_export(
        array_keys(
            array_filter(
                array_count_values($numbers),
                fn($count) => $count === 1
            )
        )
    );
    

提交回复
热议问题