Leetcode

Single Number

  • Time:O(n)
  • Space:O(1)

C++

class Solution {
 public:
  int singleNumber(vector<int>& nums) {
    int ans = 0;

    for (const int num : nums)
      ans ^= num;

    return ans;
  }
};

JAVA

class Solution {
  public int singleNumber(int[] nums) {
    int ans = 0;

    for (final int num : nums)
      ans ^= num;

    return ans;
  }
}

Python

class Solution:
  def singleNumber(self, nums: List[int]) -> int:
    return reduce(lambda x, y: x ^ y, nums, 0)