4

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

4 Answers 4

6

*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);
Sign up to request clarification or add additional context in comments.

Comments

4

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
)

Comments

1

You can do it like this: Count the occurrences of each element, then filter out the occurrences bigger than 1.

$array = [4, 3, 4, 3, 1, 2, 1];
foreach ($array as $v)
{
    $arr[$v][] = 1;  // doesn't matter if 1 is set to a different value
}
foreach($arr as $k => $v)
{
    if (count($v) == 1) {
        $o[] = $k;
    }
}    
print_r($o);

result:

Array
(
    [0] => 2
)

Comments

0

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

1 Comment

This answer will not allow the return of multiple unique values which is more likely what the OP and future visitors of this page will seek. Researchers will expect an array of non-repeated values -- this snippet generates a single value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.