Write a Python program to print duplicate elements in an array

How to write a Python program to print the duplicate elements in an array?

How to print the duplicate elements of an array?

In this program, we will print the duplicate elements present within the array. It will be executed using two loops. The primary loop will select a component and therefore the second loop will iterate through the array by comparing the chosen element with other elements. If there is any match, print the duplicate element.

What is a duplicate element?

Input: Array elements = [21, 15, 18, 21, 15, 27]

Output: Duplicate elements in the array = 21, 15

Algorithm to print duplicate elements in an array:

  • Initialize an array.
  • Duplicate elements can be discovered utilizing two loops. The outer loop will iterate through the whole array while the inner loop will compare each of its elements with those in the outer loop.
  • If any duplicate element is found it will print the element.

Python program to print duplicate elements in an array.

from array import *
arr = array('i', [])
n = int(input("enter number of elements"))
for i in range(n):
   arr.append(int(input("enter the array elements")))

print("entered array is:")
for i in range(len(arr)):
   print(arr[i])

print("Duplicate elements in given array: ")
for i in range(0, len(arr)):
   for j in range(i+1, len(arr)):
       if(arr[i] == arr[j]):
           print(arr[j])