Elements Appearing More Than Once

Find elements that appear more than once.

IntermediatePhase 4 - Level 4: Aggregation & ComparisonExample 10 of 10
elements-appearing-more-than-once.py
1# Take array
2n = int(input("Enter array size: "))
3arr = []
4for i in range(n):
5 arr.append(int(input(f"Element {i+1}: ")))
6
7# Find duplicates
8freq = {}
9for element in arr:
10 freq[element] = freq.get(element, 0) + 1
11
12# Find elements appearing more than once
13duplicates = []
14for element, count in freq.items():
15 if count > 1:
16 duplicates.append(element)
17
18print(f"Elements appearing more than once: {duplicates}")

Output

Enter array size: 6
Element 1: 1
Element 2: 2
Element 3: 2
Element 4: 3
Element 5: 3
Element 6: 3
Elements appearing more than once: [2, 3]

What's going on

Count frequency and filter duplicates.

Key Concepts:

Count frequency of each element
Check if count > 1
Collect duplicates