...

/

Solution: Find Duplicates in a List With No Repetition

Solution: Find Duplicates in a List With No Repetition

Let's look at the various solutions to find the duplicates in a list with no repetition.

Solution #1: brute force

def find_duplicates(lst):
"""
Function to find duplicates in a given lst
:param lst: A list of integers
:return: A list of duplicate integers with no repetition
"""
result = [] # A list to store duplicates
for i in range(len(lst)):
for j in range(i+1, len(lst)): # Starting from i + 1
if lst[i] == lst[j] and lst[i] not in result:
result.append(lst[i])
return result
# Driver to test above code
if __name__ == '__main__':
lst = [1, 2, 5, 1, 4, 2, 1, 1]
result = find_duplicates(lst)
print (result)

Explanation

The solution takes one element from the list, compares it with every other element (starting from i + 1), and checks if they both are equal. If they are, it adds it to the resultant list if the number is not present already.

Time complexity

The list is iterated once for each element present, so the time complexity is in O ...

Access this course and 1400+ top-rated courses and projects.