Home >>Python Programs >How to Print duplicate elements in array in python
In this example, we will see a Python program using which we can print all the duplicate elements of a given array. This can be done through two loops. The first loop will select an element and the second loop will iteration through all the array by comparing the selected element with other elements. If a match is found then it will print the duplicate element.
Example :
#Initialize array
arr = [1, 2, 3, 4, 2, 7, 8, 3, 7, 5, 1, 8];
print("Duplicate elements in given array: ");
#Searches for duplicate element
for i in range(0, len(arr)):
for j in range(i+1, len(arr)):
if(arr[i] == arr[j]):
print(arr[j]);