在数组中查找非重复元素 [英] Find the Non-Repeating Elements in an Array

查看:124
本文介绍了在数组中查找非重复元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数组是:

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

我想将其输出如下:

Output = 2 

(因为2仅出现一次)

这是我尝试过的:

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

解决方案

无循环单线:(演示)

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

明细:

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

如果您(或以后的读者)希望保留更多值,请替换array_intersect()中的第二个参数/数组. 例如: 您想保留1,2和3:array(1,2,3)[1,2,3]

p.s.作为记录,您可以将array_filter()与自定义函数一起使用,以忽略所有非1的计数值,但是我使用了array_intersect(),因为语法更简短并且IMO更易于阅读.


p.s.以为我会重新考虑并加入PHP7.4技术,并与其他基于函数的技术进行比较...

代码:(演示)

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

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

解决方案

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

这篇关于在数组中查找非重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆