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

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. python3迭代器

    一.前提 1.dir()函数 dir()函数带参数时,返回参数的属性和方法列表:不带参数时,返回当前范围内变量.方法和定义的类型列表 # dir(参数):带参数,返回参数的属性和方法 s = '' p ...

  2. Http协议 Content-Type

    详情:https://www.cnblogs.com/ranyonsue/p/5984001.html *****Referer:包含一个URL,用户从该URL代表的页面出发访问当前请求的页面. ** ...

  3. 异数OS-织梦师-PBFT(六) 走出区块链,加速破解PBFT

    . 异数OS-织梦师-PBFT(六) 走出区块链,加速破解PBFT 拜占庭 本文来自异数OS社区 github: https://github.com/yds086/HereticOS 异数OS社区Q ...

  4. 【java基础】妙记进制转换

    一.二进制与十进制 1.正整数十进制转二进制 口诀:除二取余,倒序排列 解释:将一个十进制数除以二,得到的商再除以二,依此类推直到商等于一或零时为止,倒取将除得的余数,即换算为二进制数的结果 例如把5 ...

  5. 面向初学者的指南:创建时间序列预测 (使用Python)

    https://blog.csdn.net/orDream/article/details/100013682 上面这一篇是对 https://www.analyticsvidhya.com/blog ...

  6. selenium,xpath路径中引入变量

    比如,我需要获取每一条微博的阅读数,总不可能所有微博都找出xpath,然后获取阅读数 找规律 “//*[@id='Pl_Official_MyProfileFeed__20']/div/div[2]/ ...

  7. 请求参content-type的值为json,返回报错的解决方法

    如上图,请求后报参数错误 原因content-type的值为json requests.post左侧的data要改为json 即r = requests.post(url, json=data, he ...

  8. 矩阵matrix变换的用法(css3属性transform: matrix)

    参数 2D矩阵的表示 matrix(a,b,c,d,e,f),其中6个参数在矩阵的分布: -- -- | a c e | | b d f | | 0 0 1 | -- -- 在CSS3中矩阵的原始值是 ...

  9. CTF--HTTP服务--SQL注入GET参数

    开门见山 1. 扫描靶机ip,发现PCS 192.168.31.37 2. 用nmap扫描开放端口信息 3. 快速扫描全部信息 4. 探测敏感信息 5. 用浏览器打开用户登录页面 6. 使用OWASP ...

  10. layui数据表格及分页

    一.前端部分 html只需要放一个有id的div就行了,方便js获取渲染区域 <div id="data_grid" lay-filter="demo" ...