💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
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; }; ```