查找散杂数

题目

You are given an odd-length array of integers, in which all of them are the same, except for one single number.

Implement the method stray which accepts such array, and returns that single different number.

The input array will always be valid! (odd-length >= 3)

Examples:

[1, 1, 2] => 2

[17, 17, 3, 17, 17, 17, 17] => 3

题目地址: https://www.codewars.com/kata/find-the-stray-number

思路一

数组过滤,散杂数的特征是首次出现的下标和最后一次出现的下标应保持一致(仅出现一次)。

答案:

const stray = (arr) => ~~arr.filter((x) => arr.indexOf(x) === arr.lastIndexOf(x)).join('');

其中:

  • Array.prototype.filter是 ES 6 的特性。
  • 结果为数组,先变成字符串(用Array.prototype.join),再强制类型转换为数字(用~~

思路二

注意题干中提到了 odd 奇数个,可以用位运算来求不同值。

答案:

const stray = (nums) => nums.reduce((a, b) => a ^ b);

其中:

  • Array.prototype.reduce 为 ES 6 特性,可以参考 Map/Reduce 相关教程。
在 GitHub 上编辑本页面 更新时间: Mon, Apr 10, 2023