Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions strings/remove_duplicate.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
def remove_duplicates(sentence: str) -> str:
"""
Remove duplicates from sentence
>>> remove_duplicates("Python is great and Java is also great")
'Java Python also and great is'
>>> remove_duplicates("Python is great and Java is also great")
'Java Python also and great is'
Remove duplicates from sentence and return words in sorted order

Args:
sentence: Input string containing words separated by spaces

Returns:
String with unique words sorted alphabetically

Examples:
Basic functionality:
>>> remove_duplicates("Python is great and Java is also great")
'Java Python also and great is'

Multiple spaces handling:
>>> remove_duplicates("Python is great and Java is also great")
'Java Python also and great is'

Edge cases:
>>> remove_duplicates("")
''

>>> remove_duplicates(" ")
''

>>> remove_duplicates("hello")
'hello'

>>> remove_duplicates("hello hello hello")
'hello'

Mixed case (case sensitive):
>>> remove_duplicates("Python python PYTHON")
'PYTHON Python python'

Numbers and special characters:
>>> remove_duplicates("1 2 3 1 2 3")
'1 2 3'

>>> remove_duplicates("hello world hello world!")
'hello world world!'

Single character words:
>>> remove_duplicates("a b c a b c")
'a b c'

Mixed content:
>>> remove_duplicates("The quick brown fox jumps over the lazy dog")
'The brown dog fox jumps lazy over quick the'
"""
return " ".join(sorted(set(sentence.split())))

Expand Down