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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?