136. Single Number

1.描述

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?

2.分析

x ^ x == 0

3.代码

int singleNumber(int* nums, int numsSize) {
    if (NULL == nums || numsSize <= 0) exit(-1);
    int single = nums[0];
    for (unsigned int i = 1; i < numsSize; ++i) {
        single ^= nums[i];
    }
    return single;
}

你可能感兴趣的:(136. Single Number)