Find an integer that occurs only once in an array

#There is two ways of doing this use a hash(dict) to parse the array and store the key and value. #Then loop through the hash and return the key whose value is 1

#Second way is to use the XOR operation and return the whatever value is remaining. The logic is #whenever you XOR an integer with itself it gives a 0 and 0 XOR’ed with integer is the integer #itself.

def  lonelyInteger( arr):

   value = 0

   for element in arr:

        value = value ^ element

   return value

print lonelyInteger([1])


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s