Find The Parity Outlier

本文介绍了一个名为FindTheParityOutlier的问题,该问题旨在从一个几乎全为偶数或奇数的整数数组中找出唯一的不同数。提供了一种高效的方法来解决这个问题,并给出了具体的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Find The Parity Outlier

Description:
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 N.
For example:
[2, 4, 0, 100, 4, 11, 2602, 36]
Should return: 11
[160, 3, 1719, 19, 11, 13, -21]
Should return: 160


function findOutlier(integers){
  //your code here
  var hash_flag =[1,1,1,0,1,0,0,0];
  for(var i=0;i<integers.length;i++)
  {
    if((integers[i]&1) == hash_flag[(integers[0]&1)*4+(integers[1]&1)*2+(integers[2]&1)*1])
      return integers[i];
  }
}

高票答案:

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];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值