Remove Duplicate Words
Remove duplicate words from string.
IntermediatePhase 5 - Category 3: Word OperationsExample 9 of 10
remove-duplicate-words.py
1# Take string input2s = input("Enter a string: ")34# Remove duplicate words5words = s.split()6unique_words = []7seen = set()8for word in words:9 if word not in seen:10 unique_words.append(word)11 seen.add(word)1213result = " ".join(unique_words)14print(f"Without duplicates: {result}")
Output
Enter a string: hello world hello programming Without duplicates: hello world programming
What's going on
Keep only first occurrence of each word.
Key Concepts:
Use set to track seen words
Add to result if not seen
Preserves order
🔗Related Content
- 📖
Phase 5 - Learn Concepts
Review Phase 5 concepts and explanations
- 📝
Phase 5 - Quiz
Test your Phase 5 understanding with quiz questions
- 💻
Phase 5 - All Practice Problems
Explore all practice problems for Phase 5
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum