NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Example 1: ``` Input: [2,2,1] Output: 1 ``` Example 2: ``` Input: [4,1,2,1,2] Output: 4 ``` ``` // 如果a、b两个值不相同,则异或结果为1。如果a、b两个值相同,异或结果为0。 // 利用0和任何数异或都为该数定理 var singleNumber = function(nums) { sum=0; for(var i=0;i<nums.length;i++){ sum^=nums[i]; } return sum; }; ```