PU Single number

Jan 01, 1970

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?

C Solution:

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

Python Solution:

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return reduce(operator.xor, nums);

Summary:

  1. 6ms, 32.17%
  2. Same as following Problem 389. Find the Difference.

LeetCode: 136. Single Number