Find the odd int

Given an array, find the int that appears an odd number of times.

There will always be only one integer that appears an odd number of times.

题目意思是给定一个数组,找出出现的次数为奇数的数字

常规思路就是把这个数组先排序,然后遍历数组,计算每个数字出现的数字,最后返回出现次数为奇数的数字。

偶然发现一个很巧妙的解法:

function findOdd(A) {
  return A.reduce(function(a,b){return a^b});
}

第一眼看到,居然还有这种操作?然后多看了几眼,厉害,简直了,利用异或操作符,这操作简直6到飞起。赶紧写篇博客记下,拯救下脑子不太好使的寄几。。说不定哪天开窍了呢。

原文地址:https://www.cnblogs.com/renbo/p/7846982.html