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

Check Uniqueness

Check whether every list element is unique. Students compare list length with set size and understand how repeated values disappear during set conversion.

IntermediatePhase 4 - Level 2: Searching & Counting LogicExample 5 of 10
check-uniqueness.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# Check uniqueness
8if len(arr) == len(set(arr)):
9 print("All elements are unique")
10else:
11 print("Array contains duplicate elements")

Output

Enter array size: 5
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
Element 5: 5
All elements are unique

Enter array size: 4
Element 1: 1
Element 2: 2
Element 3: 1
Element 4: 3
Array contains duplicate elements

In this one

  • Compare list and set sizes
  • Detect duplicates
  • Ignore set iteration order

How it works

  1. 1

    Build a set from arr.

  2. 2

    Compare its length with len(arr).

  3. 3

    Print unique only when the sizes agree.

Edge cases

Is an empty list unique?

Is an empty list unique?

Yes. Both lengths are zero.

Does [2, 2] pass?

Does [2, 2] pass?

No. The list length is two but the set size is one.

What's going on

set(arr) keeps one copy of each distinct integer. Comparing its size with the original list length reveals whether any positions were duplicates.

If sizes match, all values were unique. If the set is smaller, at least one value repeated. The code does not rely on set iteration order or modify arr. This size comparison uses extra storage for the distinct values.

Is an empty list unique? Yes. Both lengths are zero.

Does [2, 2] pass? No. The list length is two but the set size is one.

Quiz

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

Question 1

What does set remove?

Pick an answer to check it.

Question 2

Is an empty list unique?

Pick an answer to check it.

Questions people ask

Is an empty list unique?

Yes. Both lengths are zero.

Does [2, 2] pass?

No. The list length is two but the set size is one.

What does set remove?

Duplicate values.