Skip to content

Instantly share code, notes, and snippets.

@flavienbwk
Last active January 24, 2019 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flavienbwk/64f8b602b102426a426247f7205a0668 to your computer and use it in GitHub Desktop.
Save flavienbwk/64f8b602b102426a426247f7205a0668 to your computer and use it in GitHub Desktop.
Coding interview problem : returns the same numbers present in each given list.
# Find the numbers that appear in all arrays.
# Return them as a list.
#
# Input :
# [2, 6, 9, 11, 13, 17]
# [3, 6, 7, 10, 13, 18]
# [4, 5, 6, 9, 11, 13]
#
# Output must be :
# [6, 13]
def intersectValues(lists):
k = len(lists)
hashtable = {}
output = []
for lc in range(0, len(lists)):
list = lists[lc]
for i in range(0, len(list)):
number = list[i]
if number not in hashtable:
hashtable[number] = []
for a in range(0, k):
hashtable[number].append(1 if a == lc else 0)
else:
if hashtable[number][lc] == 0:
hashtable[number][lc] = 1
for number in hashtable:
if min(hashtable[number]) == 1:
output.append(number)
return output
print intersectValues([[2, 6, 9, 11, 13, 17], [3, 6, 7, 10, 13, 18], [4, 5, 6, 9, 11, 13]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment