
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to print the duplicate elements of an array
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.
- Related Articles
- Python Program to Remove Duplicate Elements From an Array
- Python program to print the elements of an array in reverse order
- Java Program to Print the Elements of an Array
- Python program to print the elements of an array present on odd position
- Python program to print the elements of an array present on even position
- C Program to delete the duplicate elements in an array
- Swift Program to Remove Duplicate Elements From an Array
- Golang Program To Remove Duplicate Elements From An Array
- Python program to print an Array
- C# program to find all duplicate elements in an integer array
- Python Program to Rotate Elements of an Array
- Python program to left rotate the elements of an array
- Python program to right rotate the elements of an array
- Python program to print all distinct elements of a given integer array.
- How to duplicate elements of an array in the same array with JavaScript?

Advertisements