LoginSignup

Count Duplicates in List (Python)

Asked 24 days agoAnswer 1Votes 1

1

🔁 Count Duplicates in List

❓ Problem Statement

Write a Python function that takes a list of integers and returns the number of duplicate values in it.
You should count each duplicated value only once, regardless of how many times it appears.


🔢 Input Format


📤 Output Format


✅ Constraints

  • 1 ≤ Length of list ≤ 10^4
  • -10^6 ≤ List elements ≤ 10^6

📌 Example Input

1 2 3 4 2 3 5 6 1


🎯 Example Output


🧩 Explanation

Duplicates are: 1, 2, 3 → count is 3


Count Duplicates in List (Python)

nice one - Rick Harrison 16 days ago



1 Answers

2

To solve this, we convert the list into a set (to get unique values), and then count how many of those values appear more than once in the original list.


nums = list(map(int, input().split()))

print(len([x for x in set(nums) if nums.count(x) > 1]))

Input: 1 2 3 2 4 5 3 1

Output: 3




Your Answer