Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the Non-Repeating Elements in an Array

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 is present only once)

This is what I've tried:

$array = array(4, 3, 4, 3, 1, 2, 1);
$array1 = array(4, 3, 4, 3, 1, 2, 1);
$array_diff = array_diff($array, $array1); 
like image 396
vijay Avatar asked Oct 17 '25 16:10

vijay


2 Answers

*Read last section of this an for the most stable technique the avoids fringe case issues -- it is also the most verbose.

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
        )
    )
);

*For similar tasks which have values which are not guaranteed to be integers, array_count_values() will complain with "Warning: array_count_values(): Can only count string and integer values".
Even a classic loop that uses values as first level keys like @LF00's answer will suffer potential side effects due to floats and numeric values being cast as integers automatically.

This means that a more general-use solution would be: (Demo)

$result = [];
foreach ($array as $index => $value) {
    foreach ($array as $i => $v) {
        if ($value === $v && $index !== $i) {
            continue 2; // duplicate found, stop checking this value; do not retain
        }
    }
    $result[] = $value;
}
var_export($result);
like image 101
mickmackusa Avatar answered Oct 19 '25 07:10

mickmackusa


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
)
like image 41
JazZ Avatar answered Oct 19 '25 05:10

JazZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!