dongyao2001 2017-05-24 13:36
浏览 69
已采纳

找到两个在数组中相等的值

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

How to find out the pair from this array ?
Keep in mind that the pair from array could be any number (1,2,4,3,2) or (3,3,1,2,4); I just give an random example above.

if there is a pair in array
   echo "The pair number is 1";
  • 写回答

6条回答 默认 最新

  • doufuxing8691 2017-05-24 13:42
    关注

    All of my methods will return the desired result as long as there IS a duplicate. It is also assumed because of your sample input, that there is only 1 duplicate in the array. The difference between my methods (and the other answers on this page) will be milliseconds at most for your input size. Because your users will not be able to distinguish between any of the correct methods on this page, I will suggest that the method that you implement should be determined by "readability","simplicity", and/or "brevity". There are many coders who always default to for/foreach/while loops. There are others who always defer to functional iterators. Your choice will probably just come to down to "your coding style".

    Input:

    $arr=[1,1,2,3,4];
    

    Method #1: array_count_values(), arsort(), key()

    $result=array_count_values($arr);
    arsort($result);                 // this doesn't return an array, so cannot be nested
    echo key($result);
    // if no duplicate, this will return the first value from the input array
    

    Explanation: generate new array of value occurrences, sort new array by occurrences from highest to lowest, return the key.


    Method #2: array_count_values(), array_filter(), key()

    echo key(array_filter(array_count_values($arr),function($v){return $v!=1;}));
    // if no duplicate, this will return null
    

    Explanation: generate the array of value occurrences, filter out the 1's, return the lone key.


    Method #3: array_unique(), array_diff_key(), current()

    echo current(array_diff_key($arr,array_unique($arr)));
    // if no duplicate, this will return false
    

    Explanation: remove duplicates and preserve the keys, find element that went missing, return the lone value.

    Further consideration after reading: https://www.exakat.io/avoid-array_unique/ and the accepted answer from array_unique vs array_flip I have a new favorite 2-function one-liner...

    Method #4: array_count_values(), array_flip()

    echo array_flip(array_count_values($arr))[2];
      // if no duplicate, this will show a notice because it is trying to access a non-existent element
      // you can use a suppressor '@' like this:
      // echo @array_flip(array_count_values($arr))[2];
      // this will return null on no duplicate
    

    Explanation: count the occurrences (which makes keys of the values), swap the keys and values (creating a 2-element array), access the 2 key without a function call. Quick-smart!

    If you wanted to implement Method #4, you can write something like this:(demo)

    $dupe=@array_flip(array_count_values($arr))[2];
    if($dupe!==null){
        echo "The pair number is $dupe";
    }else{
        echo "There were no pairs";
    }
    

    There will be many ways to achieve your desired result as you can see from all of the answers, but I'll stop here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改