Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Un solo numero

Dado un conjunto de enteros no vacío , cada elemento aparece dos veces, excepto uno. Encuentra ese solo. Nota: Su algoritmo debe tener una complejidad lineal de tiempo de ejecución. ¿Podría implementarlo sin usar memoria adicional?

Ejemplos :

Entrada: [2,2,1] Salida: 1

Entrada: [4,1,2,1,2] Salida: 4

 def singleNumber2(nums):
  no_duplicate_list = []
  for i in nums:
      if i not in no_duplicate_list:
          no_duplicate_list.append(i)
          print('-------- if -------')
          print(no_duplicate_list)
      else:
          no_duplicate_list.remove(i)
          print('-------- else -------')
          print(no_duplicate_list)
  #return no_duplicate_list.pop()
  print (no_duplicate_list.pop())


def singleNumber3(nums):
  hash_table = {}
  
  for i in nums:
    try :
      hash_table.pop(i)
      print('-------- try -------')
      print(hash_table)
    except:
      hash_table[i] = 1
      print('-------- except -------')
      print(hash_table)
  #return hash_table.popitem()[0]
  print(hash_table.popitem()[0])
  print(hash_table)

def singleNumber4(nums):
  res = 0
  for n in nums:
      res = res ^ n
  print( res )    

nums = [4,1,2,1,2,5,5,4,6,]

singleNumber4(nums)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.