Single Number I

Given an array of integers, every element appears twice except for one. 

Find that single one.
Your algorithm should have a linear runtime complexity. 
Could you implement it without using extra memory?

Solution: XOR.

 1 class Solution {
 2 public:
 3     int singleNumber(int A[], int n) {
 4         int single = 0;
 5         for(int i = 0; i < n; i++) {
 6             single ^= A[i];
 7         }
 8         return single;
 9     }
10 };

 

posted @ 2014-04-05 00:49  beehard  阅读(112)  评论(0编辑  收藏  举报