# Challenge me - Shortest possible answer

• In Python

``````def singleNumber(self, A):
return reduce(lambda x,y:x^y,A)``````

• "Shortest possible answer.. In Python"

That's cheating.

• Could you briefly explain your code, especially x^y part? Thanks.

• here is how reduce function works in Python:
{The function reduce(func, seq) continually applies the function func() to the sequence seq. It returns a single value. }
see diagram at http://www.python-course.eu/lambda.php

lambda x,y:x^y ----> this creates a function object that xors two arguments. just a short way of defining a function. For instance following is valid solution as well:
{
def singleNumber(self, A):
return reduce(self.xor, A)

def xor(self, x,y):
return x^y
}

• x^y simply does bitwise XOR operation

• Actually I found a shorter one myself :) It is shorter by 2 characters :)

``````    def singleNumber(self, A):
return reduce(operator.xor,A)``````

• ``````reduce(int.__xor__, A)
``````

Shorter than @hebele by 1 character. Though limited to integers.

• But you may need to "import operator"

• This post is deleted!

• ``````return 2*sum(set(A))-sum(A)
``````

4 characters less than yours

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.