I was in an interview once, where I was asked to find the first common element between two sorted arrays, and I chose the following algo:
def elementMatch(a1, a2):
'''Find first similar element in a pair of sorted arrays'''
try:
return sorted(set(a1) & set(a2))[0]
except IndexError:
return False`
The interviewer asked about another solution, and I said nested loops could work as well, but I think the set solution may be quicker. He said that he thought internally set was probably using something like a nested loop. Is that accurate? How does set work when comparing elements?