Exercise 50
Write a Python algorithm which returns the list of elements duplicated at least 3 times within a given list L .
Solution
# function which determines the elements duplicated at least 3 times
def duplicate3 (L):
# initialization of the list of duplicated elements at least 3 times
dup3 = []
for i in range (0, len (L)):
if L.count (L [i]) >= 3 and L [i] not in dup3:
dup3.append (L [i])
return dup3
# Example
L = [3, 7, 3, 7, 12, 5, 7, 12, 31, 12, 3]
print (duplicate3 (L)) # output: [3, 7, 12]
Younes Derfoufi
my-courses.net
my-courses.net