## Saturday, April 17, 2021

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

#### 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

#Example
L = [7, 23, 5, 12, 7, 19, 23, 12, 29]
print (listDuplicate (L)) # the output is: [7, 23, 12]``````

Younes Derfoufi
my-courses.net

## Python Exercises With Solutions

1. Python Arithmetic Exercises
2. Equations & System Of Equations
3. Polynomials and Functions
4. Python Arithmetic Exercises
5. Numpy, Matplolib, Sympy , Scipy - Exercises
6. Python Arithmetic Exercises

Younes Derfoufi

## Sample Text 