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.
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Check uniqueness8if 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
Build a set from arr.
- 2
Compare its length with len(arr).
- 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.
๐Related Content
- ๐
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- ๐
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- ๐ป
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum