Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Elements Appearing More Than Once

List values that occur more than once using a frequency dictionary. Students count first, filter second, and print each repeated value only once in the result.

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]

In this one

  • Filter a frequency map
  • List repeated values once
  • Keep first-appearance ordering

How it works

  1. 1

    Count every element in freq.

  2. 2

    Inspect each key-count pair.

  3. 3

    Append the key when its count exceeds one.

Edge cases

Does [2, 2, 2] output three twos?

Does [2, 2, 2] output three twos?

No. It outputs the value two once.

What if all values are different?

What if all values are different?

duplicates is empty.

What's going on

The first traversal records a frequency for each integer. The second traversal visits dictionary entries and selects counts greater than one.

A value repeated five times contributes one entry to duplicates, not four or five. Dictionary insertion order makes results follow the first appearance of each repeated value. The source array is unchanged; no sorting step is used.

Does [2, 2, 2] output three twos? No. It outputs the value two once.

What if all values are different? duplicates is empty.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which test identifies repeated values?

Pick an answer to check it.

Question 2

Does [2, 2, 2] output three twos?

Pick an answer to check it.

Questions people ask

Does [2, 2, 2] output three twos?

No. It outputs the value two once.

What if all values are different?

duplicates is empty.

Which test identifies repeated values?

count > 1.