Find the Non-Repeating Elements in an Array

后端 未结 4 1825
别跟我提以往
别跟我提以往 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
            )
        )
    );
    
    0 讨论(0)
  • 2021-01-23 05:03

    You could use the array_count_values() php function.

    For example :

    $numbers = [4, 3, 4, 3, 1, 2, 1];
    
    // build count array as key = number and value = count
    $counter_numbers = array_count_values($numbers);
    
    print_r($counter_numbers);
    

    Output :

    Array
    (
        [4] => 2
        [3] => 2
        [1] => 2
        [2] => 1
    )
    

    Then loop through the new array to get non-repeated values :

    $unique_numbers = [];
    
    foreach ($counter_numbers as $number => $count) {
        if ($count === 1) {
            $unique_numbers[] = $number;
        }
    }
    
    print_r($unique_numbers);
    

    Output :

    Array
    (
        [0] => 2
    )
    

    Hope it helps.

    0 讨论(0)
  • 2021-01-23 05:06

    If in your scenario there will be only one unique value you could use:

    $array= array(4,3,4,3,1,2,1);
    $singleValue = array_search(1, array_count_values($array));
    var_dump($singleValue) // Outputs: 2
    
    0 讨论(0)
  • 2021-01-23 05:15

    you can do it like this:

    $array= array(4,3,4,3,1,2,1);
    foreach($array as $v)
    {
      $arr[$v][] = 1;
    }
    foreach($arr as $k => $v)
    {
      if(count($v) == 1)
        $o[] = $k;
    }
    
    print_r($o);
    

    result:

    Array
    (
        [0] => 2
    )
    
    0 讨论(0)
提交回复
热议问题