Single Number
Given an 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?
进行一次扫描,把所有数据异或在一起,最终结果就是所求结果。
1 int singleNumber(int A[], int n) { 2 // Note: The Solution object is instantiated only once and is reused by each test case. 3 int result = 0; 4 int i; 5 for(i = 0; i < n; i++){ 6 result ^= A[i]; 7 } 8 return result; 9 }