# Solution Exercise 22: python algorithm which determines the list of duplicate elements

Apr 17, 2021

#### Exercise 22

Write an algorithm in python that returns the list of duplicate elements of a given list without using any predefined functions in Python. Example if L = [7, 23, 5, 12, 7, 19, 23, 12, 29], the algorithm returns the list [7, 23, 12].

#### Solution

``def listDuplicate (L):     # initialization of the list of duplicate elements     duplicateElements = []         for x in L:          # initialize the occurrence of x in L          occ_x = 0          for y in L:               if x == y:                    occ_x = occ_x + 1                          # test if x is a duplicate element and add it to the duplicateElements list          if occ_x >= 2 and x not in duplicateElements:               duplicateElements.append (x)         return duplicateElements#ExampleL = [7, 23, 5, 12, 7, 19, 23, 12, 29]print (listDuplicate (L)) # the output is: [7, 23, 12]``

Younes Derfoufi
my-courses.net