Single Number

07/13/2016 Hash Table Bit Manipulation

Question

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?


Solution

Result: Accepted Time: 8 ms

Here should be some explanations.

int singleNumber(int* nums, int numsSize) {
    int ans = 0;
    for(int i = 0; i < numsSize; i++)
        ans ^= nums[i];
    return ans;
}

Complexity Analytics

  • Time Complexity:
  • Space Complexity: