Skip to content

Instantly share code, notes, and snippets.

@galamdring
Created December 3, 2019 20:53
Show Gist options
  • Select an option

  • Save galamdring/f16b882fda3280323b27b7120dc53d3f to your computer and use it in GitHub Desktop.

Select an option

Save galamdring/f16b882fda3280323b27b7120dc53d3f to your computer and use it in GitHub Desktop.
Find a odd main out in a list of paired ints
import time
from random import randint
class FindTheSingleton:
# This creates our array, adding each random number twice then appending the non-paired number
def generateArray(length, singleton):
if length % 2 == 0 :
# increase the length to be odd, so we can fit all pairs except the singleton.
length = length+1
counter = 0
newArray = [] * length
while counter < (length-1):
newNum = randint(0, 100000000)
newArray.append(newNum)
newArray.append(newNum)
counter = counter+2
newArray.append(singleton)
return newArray
# We use this to reorder the array to add validity to the tests. This is based on the Fisher-Yates shuffle.
def shuffleArray(self, toShuffle):
n = len(toShuffle)
# Start from the last element and swap one by one. We don't
# need to run for the first element that's why i > 0
for i in range(n - 1, 0, -1):
# Pick a random index from 0 to i
j = randint(0, i + 1)
# Swap element at i with the element at random index
toShuffle[i], toShuffle[j] = toShuffle[j], toShuffle[i]
return toShuffle
def XorSolution(self, listInts):
result =0
for number in listInts:
result = number ^ result
return result
def setSolution(self, listInts):
resultSet = set()
for num in listInts:
if num in resultSet:
resultSet.remove(num)
else:
resultSet.add(num)
# At this point, we had added each number the first time we see it, and remove it when we see it again.
# This should leave us with one in the set, but we still have to iterate over it.
for result in resultSet:
return result
def dictionarySolutionTryExcept(self, listInts):
resultDict = {}
for number in listInts:
try:
resultDict[number] += 1
except KeyError:
resultDict[number] = 1
for key, value in resultDict.items():
if value == 1:
return key
def dictionarySolutionContains(self, listInts):
resultDict = {}
for number in listInts:
if number in resultDict:
resultDict[number] += 1
else:
resultDict[number] = 1
for key, value in resultDict.items():
if value == 1:
return key
# Test our solutions
findTheSingleton = FindTheSingleton()
startTime = time.time()
theArray = findTheSingleton.generateArray(999999, 11)
print(str.format("Took {} ms to build the array.", (time.time()-startTime)*1000))
startTime = time.time()
theArray = findTheSingleton.shuffleArray(theArray)
print(str.format("Took {} ms to shuffle the array.", (time.time()-startTime)*1000))
startTime = time.time()
if findTheSingleton.XorSolution(theArray) == 11:
print(str.format("Xor solution took {} ms", (time.time()-startTime)*1000))
startTime = time.time()
if findTheSingleton.dictionarySolutionContains(theArray) == 11:
print(str.format("Dictionary solution using contains took {} ms", (time.time()-startTime)*1000))
startTime = time.time()
if findTheSingleton.dictionarySolutionTryExcept(theArray) == 11:
print(str.format("Dictionary solution using try except took {} ms", (time.time()-startTime)*1000))
startTime = time.time()
if findTheSingleton.setSolution(theArray) == 11:
print(str.format("Set solution took {} ms", (time.time()-startTime)*1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment