Open In App

Program to Print Duplicates from a List of Integers in Python

Last Updated : 01 Nov, 2025
Comments
Improve
Suggest changes
64 Likes
Like
Report

Given a list of integers, the task is to identify and print all elements that appear more than once in the list. For Example: Input: [1, 2, 3, 1, 2, 4, 5, 6, 5], Output: [1, 2, 5]. Below are several methods to print duplicates from a list in Python.

Using Collections.Counter

Counter class from the collections module provides a way to count occurrences and easily identify duplicates.

Python
from collections import Counter

a = [1, 2, 3, 1, 2, 4, 5, 6, 5]
count = Counter(a)

res = [num for num, freq in count.items() if freq > 1]
print(res)

Output
[1, 2, 5]

Explanation:

  • Counter(a) counts how many times each number appears.
  • list comprehension extracts only the elements with frequency greater than 1.

Using Set

A set in Python stores only unique elements. By tracking seen elements in a set, duplicates can be identified efficiently.

Python
a = [1, 2, 3, 1, 2, 4, 5, 6, 5]
s = set()
dup = []

for n in a:
    if n in s:
        dup.append(n)
    else:
        s.add(n)
print(dup)

Output
[1, 2, 5]

Explanation:

  • s keeps track of the elements that have already appeared.
  • If an element n is already in s, it’s added to dup as a duplicate.

Using a Dictionary

A dictionary can be used to count the occurrences of each element. Any element with a count greater than 1 is a duplicate.

Python
a = [1, 2, 3, 1, 2, 4, 5, 6, 5]
count = {}
for n in a:
    count[n] = count.get(n, 0) + 1

duplicates = [n for n, c in count.items() if c > 1]
print(duplicates)

Output
[1, 2, 5]

Explanation:

  • count.get(n, 0) + 1: updates each element’s frequency.
  • The list comprehension selects elements with a count greater than 1.

Using Nested Loops

This method compares every element with all others to find duplicates. It’s easy to understand but less efficient for larger lists.

Python
a = [1, 2, 3, 1, 2, 4, 5, 6, 5]
dup = []

for i in range(len(a)):
    for j in range(i + 1, len(a)):

        if a[i] == a[j] and a[i] not in dup:
			
            dup.append(a[i]) 
print(dup)

Output
[1, 2, 5]

Explanation:

  • if a[i] == a[j] and a[i] not in dup: Checks if the element is a duplicate and not already added.
  • dup.append(a[i]): Adds the duplicate element to the list.

Article Tags :

Explore