Efficiently determine if two of three items in a list are the same
algorithm, list, python
Solution
You can see how many unique values are there with a `set`. If there is one less item in the set than in the list, one was a duplicate:
def has1dup(lst):
return len(lst)-1 == len(set(lst))
Problem
What is the most efficient way to determine if exactly two elements in a list are the same? For example: ``` >>> has1dup(["one", "one", "two"]) True >>> has1dup(["one", "two", "three"]) False >>> has1dup(["one", "one", "one"]) False ``` I have successfully done this using if/else statements. However, if the list were larger, the task of writing out each possibility for a pair would become very difficult and time consuming. Is there a faster/simpler way to accomplish this? Here is what I have tried: ``` def has1dup(lst): if lst[0] == lst[1] and lst[1] != lst[2]: return True elif lst[1] == lst[2] and lst[2] != lst[0]: return True elif lst[0] == lst[2] and lst[2] != lst[1]: return True else: return False ```