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 相关教程。