Given an array of strings words, return a list of all words that appear more than once in the array.
Each duplicate word should appear only once in the output. Return the result sorted in ascending lexicographic order.
Function Signature
def find_duplicate_words(words: list[str]) -> list[str]:
Rules
-
A word is a duplicate when it appears at least twice in
words
. A word that appears three or more times is still listed once.
-
Sort the output in ascending lexicographic order so the expected answer is unique.
-
If no word repeats, return an empty list.
Constraints
-
1 <= len(words) <= 10^4
-
1 <= len(words[i]) <= 100
-
words[i]
consists of lowercase English letters.
Examples
Input: words = ["apple","banana","apple","cherry","banana","date"]
Output: ["apple","banana"]
"apple" and "banana" each appear twice. "cherry" and "date" each appear once, so they are left out.
Input: words = ["hello","world","hello","hello","world","foo"]
Output: ["hello","world"]
"hello" appears three times and is still listed once. "world" appears twice, and "foo" appears only once.
Input: words = ["pear","kiwi","pear","kiwi","fig"]
Output: ["kiwi","pear"]
"pear" and "kiwi" each appear twice. "pear" comes first in the input, but the output is sorted, so "kiwi" is listed first.