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?ios
分析:採用異或的方法查找;數組
#include <iostream> using namespace std; int singleNumber(int A[], int n) { int x = A[0]; for (int i = 1; i < n; ++i) { x ^= A[i]; } return x; } int main() { int a[7]={1,3,4,4,3,2,1}; int ans=singleNumber(a,7); cout<<"the ans is "<<ans; return 0; }