Define an array of non-empty integers in which every element appears twice except for one element. Find the element that appears only once.
Description:
Your algorithm should have linear time complexity. Can you do it without using extra space?
Example 1:
Input: [2,2,1] Output: 1 Example 2: Input: [4,1,2,1] Output: 4Copy the code
Source: LeetCode link: leetcode-cn.com/problems/si…
Answer:
A ^b^c=c^b^a, nums[I], nums[I], nums[I], nums[I], nums[I], nums[I], nums[I
Enter,1,2,1,2 [4], for example, 0 ^ 4 ^ 1 ^ 2 ^ 1 ^ 2 = 4 ^ 1 ^ 1 ^ 2 ^ 2 = 4 ^ ^ ^ ^ 0 0 0 0 = 4
var singleNumber = function(nums) {
let num =0;
for(let i =0; i<nums.length; i++){ num^=nums[i] }return num
}
Copy the code