美文网首页
[6kyu]Find The Parity Outlier

[6kyu]Find The Parity Outlier

作者: 君肄塵 | 来源:发表于2017-07-24 16:48 被阅读47次

    该算法题来自于 codewars【语言: javascript】,翻译如有误差,敬请谅解~

    • 任务
    • 编写一个函数 findOutlier,参数是一个数组,返回数组中单独的奇数或是偶数。
    • 例如:
      findOutlier([2, 4, 0, 100, 4, 11, 2602, 36]) // 11
      findOutlier([160, 3, 1719, 19, 11, 13, -21]) // 160

    • 解答
    • 其一
    const findOutlier = integers => integers.filter(el=>el%2).length == 1 ? integers.filter(el=>el%2)[0] : integers.filter(el=>!(el%2))[0];
    
    • 其二
    function findOutlier(int){
          var even = int.filter(a=>a%2==0);
          var odd = int.filter(a=>a%2!==0);
          return even.length==1? even[0] : odd[0];
    }
    

    相关文章

      网友评论

          本文标题:[6kyu]Find The Parity Outlier

          本文链接:https://www.haomeiwen.com/subject/dlbbkxtx.html