Program to Print Duplicates from a List of Integers in Python (original) (raw)

Last Updated : 1 Nov, 2025

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)

`

**Explanation:

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)

`

**Explanation:

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)

`

**Explanation:

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)

`