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?
input: 一個雙倍出現的數字 + 單獨出現的數字組成陣列
output: 找出那個落單的
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
*/
/**
* @param {number[]} nums
* @return {number}
*/
var singleNumber = function(nums) {}
var singleNumber = function(nums) {
let result = nums[0];
for(let i= 1; i< nums.length; i++){
result = result ^ nums[i]
}
return result;
};
console.log(singleNumber([4,1,2,1,2,2]))