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

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. [3.0] 一个人开发一个App,小程序从0到1,删减添加

    在这个黄道吉日,咱们将要干一件,惊天地泣鬼神,妇孺皆知的大事,那就是删掉微信开发工具自动生成的源代码. 删掉pages下的index.logs目录,啥都不留: 删掉utils下的util.js,只流空 ...

  2. springboot 报错nested exception is java.lang.IllegalStateException: Failed to check the status of the service xxxService No provider available for the service

    spring: dubbo:#关闭所有服务的启动时检查:(没有提供者时报错) consumer: check: false timeout: 3000

  3. 玩转Django2.0---Django笔记建站基础十二(Django项目上线部署)

    第十二章 Django项目上线部署 目前部署Django项目有两种主流方案:Nginx+uWsGI+Django或者Apache+uWSGI+Django.Nginx作为服务器最前端,负责接收浏览器的 ...

  4. web api 的 安全 认证问题 , 对外开放 的 时候 需要考虑到安全的问题

    关于 OWIN OAuth , web api的认证,全局验证, 安全方面的验证 有必要 去 自己捣鼓一下.

  5. 从零开始ming的多人联机游戏--游戏客户端(1)六边形地图

    打算做的小游戏是一个多人联机的策略类游戏,类似于<文明>那种 游戏的玩法并不确定,开这个坑主要是为了入门后端开发,顺便熟悉下游戏开发 这篇文章使用unity,实现了六边形单元地图的创建.后 ...

  6. 用 F# 手写 TypeScript 转 C# 类型绑定生成器

    前言 我们经常会遇到这样的事情:有时候我们找到了一个库,但是这个库是用 TypeScript 写的,但是我们想在 C# 调用,于是我们需要设法将原来的 TypeScript 类型声明翻译成 C# 的代 ...

  7. 解决apt-get安装软件包的时候遇到E: Sub-process /usr/bin/dpkg returned an error code (1)问题

    在用apt-get安装软件包的时候遇到E: Sub-process /usr/bin/dpkg returned an error code (1)问题,解决方法如下:   cd /var/lib/d ...

  8. hash算法与拉链法解决冲突

    <?php class HashNode { public $key; public $value; public $nextNode; public function __construct( ...

  9. ios--->tableView的估算高度的作用

    ios中tableView的估算高度的作用 在ios7之后,tableView有了估算高度的这个概念及相关属性和方法:它的作用和使用场景是什么? 在tableview加载完数据渲染之后,考虑到滚动条的 ...

  10. mac电脑下使用fcrackzip破解zip压缩文件密码

    fcrackzip简介 fcrackzip是一款专门破解zip类型压缩文件密码的工具,工具小巧方便.破解速度快,能使用字典和指定字符集破解,适用于linux.mac osx 系统 fcrackzip安 ...