问题描述:(找出奇数数组中唯一的偶数,或是偶数数组中唯一的奇数)

You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns this "outlier" N.

Examples

[2, 4, 0, 100, 4, 11, 2602, 36]
Should return: 11 (the only odd number) [160, 3, 1719, 19, 11, 13, -21]
Should return: 160 (the only even number) 我的答案:
 function findOutlier(integers){
//your code here
var odd=[];
var even=[];
for( var i=0;i<integers.length;i++){
if(integers[i]%2==0){
even.push(integers[i]);
}else{odd.push(integers[i]);}
}
if(even.length==1){return even.pop();}
else{return odd.pop();} }

优秀答案:
(1)
 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];
}

关键问题:自己在解决问题的时候不会优先想到数组的方法,filter,forEach,map,reduce。

codewars--js--Find The Parity Outlier的更多相关文章

  1. [CodeWars][JS]实现链式加法

    在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...

  2. [CodeWars][JS]实现大整数加法

    问题描述 实现‘字符串加法’,即将两个以字符串形式表示的数字相加,得到结果然后返回一个新的字符串. 例如:输入‘123’,‘321’,返回‘444’. 这样在进行两个任意大的整数相加的时候,既不会溢出 ...

  3. [CodeWars][JS]如何判断给定的数字是否整数

    问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...

  4. [Node.js] Proxy Requests for Local and Remote Service Parity

    High availability apps require that no distinction be made between local and remote services. Attach ...

  5. 全排列算法的JS实现

    问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的 ...

  6. 身份证验证JS代码

    身份证验证JS程序function checkidcardfun(code) { var city = {11: "北京", 12: "天津", 13: &qu ...

  7. js正则实现二代身份证号码验证详解

    js正则实现二代身份证号码验证详解 根据[中华人民共和国国家标准 GB 11643-1999]中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至 ...

  8. codewars 随手记

    1.ES6数组遍历语法糖=> 在C#Linq里曾经用过,因此也不是很陌生. var range = Array.apply(null, Array(x)).map((_, i) => ++ ...

  9. 递归调用里的性能问题(js)

    说明 这是在codewars.com上刷的一道js练习题,在此做个记录 问题描述 The Fibonacci sequence is traditionally used to explain tre ...

随机推荐

  1. Java单体应用 - 开发工具 - 02.Maven

    原文地址:http://www.work100.net/training/monolithic-tools-maven.html 更多教程:光束云 - 免费课程 Maven 序号 文内章节 视频 1 ...

  2. 一行代码去掉Devexpress弹窗

    使用的是.net hook方法: 使用代码: using System; using System.Windows.Forms; namespace AlexDevexpressToolTest { ...

  3. scikit-learn基础

    一.scikit-learn基础 sklearn.ensemble模块有两种基于决策树的算法----随机森林和极端随机树

  4. 18年第一弹射 和网络有关; 艾曲塞嗯诶系列篇 two

    35: 华为AR G3系列路由器可以通过FTP和TFTP更新系统文件,AR G3系列路由器可以作为FTP Client , FTP Server ,TFTP Client 36: 两台路由器间通过串口 ...

  5. lind 语 api 数据的安全性  第四弹

    web api的安全性怎么保证呢. 一般公司会自己封装一套请求的规范. 下面来看看lind语里的webapi安全规范 step one 先看下 diagram: 学而思: 从上面的图分析一下: 如果我 ...

  6. 钝化 会钝化 订单审批流程 码一会er

    先放一张订单审批流程图.预则立嘛

  7. 关于 C#和.net 的 发展

    591. C# 1 的 委托 语法 看起来 似乎 并不 太坏 [2016-04-27 09:00:56]592. C# 2 支持 从 方法 组 到 一个 兼容 委托 类型 的 隐式 转换. [2016 ...

  8. doT 这个模板 是怎么实现的?

    之前做过一个微信有关的站 模板用 doT 嗯 这个 用起来很 不错. 但是 它是怎么实现的,想过没有? ps:https://github.com/olado/doT 源码总共 140行. 第90行里 ...

  9. kafka概念扫盲

    一.kafka概述 1.1.定义 Kakfa是一个分布式的基于发布/订阅模式的消息队列(message queue),主要应用于大数据的实时处理领域 1.2.消息队列 1.2.1.传统的消息队列&am ...

  10. Windows PHP 开启opcache的方法

    PHP opcache可以提升性能.Windows PHP 配置 opcache 的方法如下: 1.先检查PHP目录下ext目录中有没有php_opcache.dll,没有的话自己下载(PHP 5.5 ...