
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Duplicate Elements of an Array in Python
When it is required to print the duplicate elements of an array, the list elements are iterated over, and a nested loop is used.
Below is a demonstration of the same −
Example
my_list = [1, 2, 5, 6, 8, 9, 3, 4, 8, 9, 1, 8] print("The list is :") print(my_list) print("The duplicate elements in the list are : ") for i in range(0, len(my_list)): for j in range(i+1, len(my_list)): if(my_list[i] == my_list[j]): print(my_list[j])
Output
The list is : [1, 2, 5, 6, 8, 9, 3, 4, 8, 9, 1, 8] The duplicate elements in the list are : 1 8 8 9 8
Explanation
A list is defined, and the elements are displayed on the console.
The list is iterated over, twice, and elements of the first and next concurrent position are compared.
If they match, that element is considered as a duplicate.
It is displayed on the console.
Advertisements