Check Uniqueness
Check if all elements are unique.
IntermediatePhase 4 - Level 2: Searching & Counting LogicExample 5 of 10
check-uniqueness.py
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
What's going on
Compare length with set length.
Key Concepts:
set() removes duplicates
If lengths equal, all unique
Otherwise, duplicates exist
🔗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