Logic Building
# Take array
n = int(input("Enter array size: "))
arr = []
for i in range(n):
arr.append(int(input(f"Element {i+1}: ")))
# Find duplicates
freq = {}
for element in arr:
freq[element] = freq.get(element, 0) + 1
# Find elements appearing more than once
duplicates = []
for element, count in freq.items():
if count > 1:
duplicates.append(element)
print(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]
Count frequency and filter duplicates.
Key Concepts:
- Count frequency of each element
- Check if count > 1
- Collect duplicates